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

master
Armando Rivera 11 years ago
parent fcc0f601a8
commit c723cb3c1c
  1. 2
      cppbas.inc
  2. 85
      sdl_test/test.cpp

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

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

Loading…
Cancel
Save