Fixee minor issue in sdl_test, tweake ENUM define to provide { automatically

This commit is contained in:
Armando Rivera 2013-11-07 14:06:36 -05:00
parent fcc0f601a8
commit c723cb3c1c
2 changed files with 44 additions and 43 deletions

View File

@ -72,7 +72,7 @@
#define VECTOR std::vector #define VECTOR std::vector
#define MAP std::map #define MAP std::map
#define CHAR char #define CHAR char
#define ENUM enum #define ENUM enum {
#define ENDENUM }; #define ENDENUM };
#define EXIT exit #define EXIT exit
#define BREAK break; #define BREAK break;

View File

@ -1,11 +1,9 @@
#include "../cppbas.inc" #include "../cppbas.inc"
#include <SDL.h> #include "sdl.inc"
#include <SDL_image.h>
#include <SDL_ttf.h>
DIM AS CHAR string[128]; // String used by txtIt & SDL_ttf DIM AS CHAR string[128]; // String used by txtIt & SDL_ttf
// Conver Variable Argument into a string // Converts Variable Argument(s) into a string
SUB txtIt(CSTRING pStr , ...) BEGIN SUB txtIt(CSTRING pStr , ...) BEGIN
DIM AS va_list valist; // Type to hold information about variable arguments DIM AS va_list valist; // Type to hold information about variable arguments
va_start(valist, pStr); // Initialize a variable argument list va_start(valist, pStr); // Initialize a variable argument list
@ -13,13 +11,13 @@ SUB txtIt(CSTRING pStr , ...) BEGIN
va_end(valist); // This macro should be executed before the function va_end(valist); // This macro should be executed before the function
ENDSUB ENDSUB
DIM AS INT FPS = 50; // Assign a FPS DIM AS INT FPS = 40; // Assign a FPS
DIM AS INT NextTick , interval ; // Used by FPS_Fn DIM AS INT NextTick , interval; // Used by FPS_Fn
// Initialize FPS_Fn( ) // Initialize FPS_Fn( )
SUB FPS_Initialize() BEGIN SUB FPS_Initialize() BEGIN
NextTick = 0 ; NextTick = 0;
interval = 1 * 1000 / FPS ; interval = 1 * 1000 / FPS;
ENDSUB ENDSUB
// Frame Per Second Function , put this in a loop // Frame Per Second Function , put this in a loop
@ -27,14 +25,14 @@ SUB FPS_Fn() BEGIN
IF ( NextTick > SDL_GetTicks( ) ) THEN IF ( NextTick > SDL_GetTicks( ) ) THEN
SDL_Delay( NextTick - SDL_GetTicks( ) ); SDL_Delay( NextTick - SDL_GetTicks( ) );
ENDIF ENDIF
NextTick = SDL_GetTicks( ) + interval ; NextTick = SDL_GetTicks( ) + interval;
ENDSUB ENDSUB
// This function load a image file to a surface // This function load a image file to a surface
// Set bCKey with colorkey (R,G,B) to clear a color on the image // Set bCKey with colorkey (R,G,B) to clear a color on the image
// Set alpha value FOR transparency 0(No transparent) ~ 255(Ivisible) // Set alpha value FOR transparency 0(No transparent) ~ 255(Ivisible)
FUNCTION SDL_Surface *ImgLoader(CSTRING file, BOOL bCKey, INT r, INT g, INT b, INT alpha) BEGIN FUNCTION PSURFACE ImgLoader(CSTRING file, BOOL bCKey, INT r, INT g, INT b, INT alpha) BEGIN
SDL_Surface *pic; DIM AS PSURFACE pic;
pic = IMG_Load(file.c_str()); // From SDL_image.h , load the image to pic pic = IMG_Load(file.c_str()); // From SDL_image.h , load the image to pic
IF (pic==NULL) THEN IF (pic==NULL) THEN
fprintf(stderr,"Missing image %s : %s\n",file.c_str(),IMG_GetError()); fprintf(stderr,"Missing image %s : %s\n",file.c_str(),IMG_GetError());
@ -50,13 +48,13 @@ FUNCTION SDL_Surface *ImgLoader(CSTRING file, BOOL bCKey, INT r, INT g, INT b, I
ENDFUNCTION ENDFUNCTION
// Load a normal picture into a surface // Load a normal picture into a surface
FUNCTION SDL_Surface *ImgLoader(CSTRING file) BEGIN FUNCTION PSURFACE ImgLoader(CSTRING file) BEGIN
RETURN ImgLoader(file.c_str(),1,0,0,0,0) ; RETURN ImgLoader(file.c_str(),1,0,0,0,0);
ENDFUNCTION ENDFUNCTION
// Load a pic & set the transparent color to (255,255,255) , no alpha // Load a pic & set the transparent color to (255,255,255) , no alpha
FUNCTION SDL_Surface *ImgLoader(CSTRING file,bool bCKey) BEGIN FUNCTION PSURFACE ImgLoader(CSTRING file,bool bCKey) BEGIN
RETURN ImgLoader(file.c_str(),1,255,255,255,0) ; RETURN ImgLoader(file.c_str(),1,255,255,255,0);
ENDFUNCTION ENDFUNCTION
@ -65,9 +63,9 @@ MAIN
DIM AS TTF_Font *font; // Declare a SDL_ttf font : font DIM AS TTF_Font *font; // Declare a SDL_ttf font : font
TTF_Init(); // Initilize SDL_ttf TTF_Init(); // Initilize SDL_ttf
font = TTF_OpenFont("./font/cour.ttf",16); // Open a font & set the font size font = TTF_OpenFont("./font/cour.ttf",16); // Open a font & set the font size
DIM AS SDL_Surface *imgTxt ; // Store image of the text FOR blit DIM AS PSURFACE imgTxt; // Store image of the text FOR blit
DIM AS SDL_Rect txtRect ; // Store (x,y) of text FOR blit DIM AS RECT txtRect; // Store (x,y) of text FOR blit
DIM AS SDL_Color fColor , fColorW , fColorR , fColorG ; // Font color (R,G,B) DIM AS COLOR fColor , fColorW , fColorR , fColorG; // Font color (R,G,B)
txtRect.x = 10; txtRect.x = 10;
txtRect.y = 250; txtRect.y = 250;
fColor.r = fColor.g = fColor.b = 245; // Set font to white color fColor.r = fColor.g = fColor.b = 245; // Set font to white color
@ -83,20 +81,21 @@ MAIN
DIM AS SDL_Event event; // FOR keyboard event DIM AS SDL_Event event; // FOR keyboard event
DIM AS BOOL bRun = 1; // The SELECT of WHILE loop DIM AS BOOL bRun = 1; // The SELECT of WHILE loop
// bKeyXXX Present the state of keyboard // bKeyXXX Present state of keyboard
DIM AS BOOL bKeyUP = 0 , bKeyDOWN = 0 , bKeyLEFT = 0 , bKeyRIGHT = 0; DIM AS BOOL bKeyUP = 0 , bKeyDOWN = 0 , bKeyLEFT = 0 , bKeyRIGHT = 0;
DIM AS INT aTick = 0; DIM AS INT aTick = 0;
DIM AS SHORT dx = 2; // The movement value when moving the object anim[ ] DIM AS SHORT dx = 2; // The movement value when moving the object anim[ ]
SDL_Surface *screen ; PSURFACE screen;
ENUM BEGIN
ENUM
pic1, pic1,
pic_No pic_No
ENDENUM ENDENUM
DIM AS SDL_Surface *anim[pic_No]; DIM AS PSURFACE anim[pic_No];
DIM AS SDL_Rect animRect ; // The coordinate of anim[ ] DIM AS RECT animRect; // The coordinate of anim[ ]
animRect.x = 160 ; animRect.x = 160;
animRect.y = 160 ; animRect.y = 160;
atexit(SDL_Quit); atexit(SDL_Quit);
@ -110,13 +109,13 @@ MAIN
FPS_Initialize( ); FPS_Initialize( );
DIM AS INT xi=1 , yi=1 ; // Use FOR display picture DIM AS INT xi=1 , yi=1; // Use FOR display picture
WHILE (bRun) BEGIN WHILE (bRun) BEGIN
aTick++; aTick++;
SDL_FillRect(screen , NULL , 0x221122); SDL_FillRect(screen , NULL , 0x221122);
IF ( aTick%6 == 1) xi++; IF ( aTick % 6 == 1) xi++;
IF ( xi > 9 ) THEN IF ( xi > 9 ) THEN
xi = 1; xi = 1;
@ -126,13 +125,13 @@ MAIN
IF ( yi > 9 ) yi = 1; IF ( yi > 9 ) yi = 1;
FOR(INT i=0 TO i < yi STEP i++ ) BEGIN FOR(INT i=0 TO i < yi STEP i++ ) BEGIN
animRect.x = 10 + i * 40 ; animRect.x = 10 + i * 40;
animRect.y = 200; animRect.y = 200;
SDL_BlitSurface( anim[0] , NULL , screen , &animRect ); SDL_BlitSurface( anim[0] , NULL , screen , &animRect );
END END
FOR (INT i=0 TO i < xi STEP i++ ) BEGIN FOR (INT i=0 TO i < xi STEP i++ ) BEGIN
animRect.x = 10 + i * 40 ; animRect.x = 10 + i * 40;
animRect.y = 240; animRect.y = 240;
SDL_BlitSurface( anim[0] , NULL , screen , &animRect ); SDL_BlitSurface( anim[0] , NULL , screen , &animRect );
END END
@ -142,16 +141,15 @@ MAIN
FOR( INT j = 1 TO j <= 9 STEP j++) BEGIN FOR( INT j = 1 TO j <= 9 STEP j++) BEGIN
txtIt("%2d" , i*j ); txtIt("%2d" , i*j );
fColor = fColorW; // Default is white color fColor = fColorW; // Default is white color
// IF ( i==1 OR j==1 ) THEN IF ( i==xi OR j==yi ) THEN fColor = fColorG;
IF ( i==1 AND j==yi ) fColor = fColorG; IF ( i==1 AND j==yi ) fColor = fColorG;
IF( j==1 AND i==xi ) fColor = fColorG; IF( j==1 AND i==xi ) fColor = fColorG;
IF( i == xi AND j == yi ) fColor = fColorR; IF( i == xi AND j == yi ) fColor = fColorR;
imgTxt = TTF_RenderText_Solid( font , string , fColor ); imgTxt = TTF_RenderText_Solid( font , string , fColor );
txtRect.x = 30 + i*30 ; txtRect.x = 30 + i*30;
txtRect.y = 30 + j*15 ; txtRect.y = 30 + j*15;
SDL_BlitSurface( imgTxt , NULL , screen , &txtRect ); SDL_BlitSurface( imgTxt , NULL , screen , &txtRect );
END // FOR( INT j = 1 ; j <= 9 ; j++) { END END
END // FOR(INT i = 1 ; i <= 9 ; i++) { END END
txtIt("%1d %1d %1d %1d", bKeyUP , bKeyDOWN , bKeyLEFT , bKeyRIGHT); txtIt("%1d %1d %1d %1d", bKeyUP , bKeyDOWN , bKeyLEFT , bKeyRIGHT);
fColor = fColorW; // Default is white color fColor = fColorW; // Default is white color
@ -163,7 +161,7 @@ MAIN
WHILE ( SDL_PollEvent( &event ) ) BEGIN WHILE ( SDL_PollEvent( &event ) ) BEGIN
SELECT ( event.type ){ SELECT ( event.type ){
CASE SDL_KEYDOWN: CASE SDL_KEYDOWN:
SELECT ( event.key.keysym.sym ){ SELECT ( event.key.keysym.sym ) BEGIN
CASE SDLK_UP: CASE SDLK_UP:
bKeyUP = 1; bKeyUP = 1;
ENDCASE ENDCASE
@ -181,8 +179,9 @@ MAIN
ENDCASE ENDCASE
CASE_ELSE: CASE_ELSE:
ENDCASE ENDCASE
ENDSELECT // SELECT ( event.key.keysym.sym ){ END ENDSELECT
// CASE SDL_KEYDOWN: END ENDCASE
CASE SDL_KEYUP: CASE SDL_KEYUP:
SELECT ( event.key.keysym.sym ){ SELECT ( event.key.keysym.sym ){
CASE SDLK_UP: CASE SDLK_UP:
@ -199,15 +198,17 @@ MAIN
ENDCASE ENDCASE
CASE_ELSE: CASE_ELSE:
ENDCASE ENDCASE
ENDSELECT // SELECT ( event.key.keysym.sym ){ END ENDSELECT
// CASE SDL_KEYUP: END ENDCASE
CASE SDL_QUIT: CASE SDL_QUIT:
bRun = 0; bRun = 0;
ENDCASE ENDCASE
CASE_ELSE: CASE_ELSE:
ENDCASE ENDCASE
ENDSELECT // SELECT ( event.type ){ END ENDSELECT
WEND // WHILE ( SDLK_PollEvent( &event ) ){ END WEND
// Deal with key states // Deal with key states
IF (bKeyUP) animRect.y = animRect.y - dx; IF (bKeyUP) animRect.y = animRect.y - dx;
@ -215,7 +216,7 @@ MAIN
IF (bKeyLEFT) animRect.x = animRect.x - dx; IF (bKeyLEFT) animRect.x = animRect.x - dx;
IF (bKeyRIGHT) animRect.x = animRect.x + dx; IF (bKeyRIGHT) animRect.x = animRect.x + dx;
WEND // WHILE (bRun) { END WEND
RETURN 0; RETURN 0;