2013-11-07 20:37:10 +00:00
|
|
|
#include "../jade.h"
|
2013-11-07 19:06:36 +00:00
|
|
|
#include "sdl.inc"
|
2013-11-07 02:52:08 +00:00
|
|
|
|
|
|
|
DIM AS CHAR string[128]; // String used by txtIt & SDL_ttf
|
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
// Converts Variable Argument(s) into a string
|
2013-11-07 04:48:25 +00:00
|
|
|
SUB txtIt(CSTRING pStr , ...) BEGIN
|
2013-11-07 02:52:08 +00:00
|
|
|
DIM AS va_list valist; // Type to hold information about variable arguments
|
|
|
|
va_start(valist, pStr); // Initialize a variable argument list
|
2013-11-07 04:48:25 +00:00
|
|
|
vsprintf( string , pStr.c_str() , valist ); // Print formatted variable argument list to string
|
2013-11-07 02:52:08 +00:00
|
|
|
va_end(valist); // This macro should be executed before the function
|
|
|
|
ENDSUB
|
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
DIM AS INT FPS = 40; // Assign a FPS
|
2013-11-11 02:22:16 +00:00
|
|
|
DIM AS UINT NextTick; // Used by FPS_Fn
|
|
|
|
DIM AS INT interval; // Used by FPS_Fn
|
2013-11-07 02:52:08 +00:00
|
|
|
|
|
|
|
// Initialize FPS_Fn( )
|
|
|
|
SUB FPS_Initialize() BEGIN
|
2013-11-07 19:06:36 +00:00
|
|
|
NextTick = 0;
|
|
|
|
interval = 1 * 1000 / FPS;
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDSUB
|
|
|
|
|
|
|
|
// Frame Per Second Function , put this in a loop
|
|
|
|
SUB FPS_Fn() BEGIN
|
|
|
|
IF ( NextTick > SDL_GetTicks( ) ) THEN
|
|
|
|
SDL_Delay( NextTick - SDL_GetTicks( ) );
|
|
|
|
ENDIF
|
2013-11-07 19:06:36 +00:00
|
|
|
NextTick = SDL_GetTicks( ) + interval;
|
2013-11-07 02:52:08 +00:00
|
|
|
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)
|
2013-11-07 19:06:36 +00:00
|
|
|
FUNCTION PSURFACE ImgLoader(CSTRING file, BOOL bCKey, INT r, INT g, INT b, INT alpha) BEGIN
|
|
|
|
DIM AS PSURFACE pic;
|
2013-11-07 04:44:16 +00:00
|
|
|
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());
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDIF
|
|
|
|
IF ( bCKey ) THEN
|
|
|
|
SDL_SetColorKey(pic,SDL_SRCCOLORKEY|SDL_RLEACCEL,SDL_MapRGB(pic->format,r,g,b));
|
|
|
|
ENDIF
|
2013-11-07 04:44:16 +00:00
|
|
|
IF (alpha) THEN
|
2013-11-07 02:52:08 +00:00
|
|
|
SDL_SetAlpha(pic, SDL_SRCALPHA|SDL_RLEACCEL , 255 - alpha);
|
|
|
|
ENDIF
|
|
|
|
pic = SDL_DisplayFormat(pic);
|
|
|
|
RETURN (pic);
|
|
|
|
ENDFUNCTION
|
|
|
|
|
|
|
|
// Load a normal picture into a surface
|
2013-11-07 19:06:36 +00:00
|
|
|
FUNCTION PSURFACE ImgLoader(CSTRING file) BEGIN
|
|
|
|
RETURN ImgLoader(file.c_str(),1,0,0,0,0);
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDFUNCTION
|
|
|
|
|
|
|
|
// Load a pic & set the transparent color to (255,255,255) , no alpha
|
2013-11-07 19:06:36 +00:00
|
|
|
FUNCTION PSURFACE ImgLoader(CSTRING file,bool bCKey) BEGIN
|
|
|
|
RETURN ImgLoader(file.c_str(),1,255,255,255,0);
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDFUNCTION
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2013-11-07 19:06:36 +00:00
|
|
|
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)
|
2013-11-07 02:52:08 +00:00
|
|
|
txtRect.x = 10;
|
|
|
|
txtRect.y = 250;
|
|
|
|
fColor.r = fColor.g = fColor.b = 245; // Set font to white color
|
|
|
|
fColorW.r = 250;
|
|
|
|
fColorW.g = 250;
|
|
|
|
fColorW.b = 250;
|
|
|
|
fColorR.r = 250;
|
|
|
|
fColorR.g = 25;
|
|
|
|
fColorR.b = 25;
|
|
|
|
fColorG.r = 25;
|
|
|
|
fColorG.g = 250;
|
|
|
|
fColorG.b = 25;
|
|
|
|
|
|
|
|
DIM AS SDL_Event event; // FOR keyboard event
|
|
|
|
DIM AS BOOL bRun = 1; // The SELECT of WHILE loop
|
2013-11-07 19:06:36 +00:00
|
|
|
// bKeyXXX Present state of keyboard
|
2013-11-07 04:44:16 +00:00
|
|
|
DIM AS BOOL bKeyUP = 0 , bKeyDOWN = 0 , bKeyLEFT = 0 , bKeyRIGHT = 0;
|
2013-11-07 02:52:08 +00:00
|
|
|
DIM AS INT aTick = 0;
|
|
|
|
DIM AS SHORT dx = 2; // The movement value when moving the object anim[ ]
|
2013-11-07 19:06:36 +00:00
|
|
|
PSURFACE screen;
|
2013-11-11 02:22:16 +00:00
|
|
|
|
|
|
|
ENUM
|
2013-11-07 04:44:16 +00:00
|
|
|
pic1,
|
|
|
|
pic_No
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDENUM
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
DIM AS PSURFACE anim[pic_No];
|
|
|
|
DIM AS RECT animRect; // The coordinate of anim[ ]
|
|
|
|
animRect.x = 160;
|
|
|
|
animRect.y = 160;
|
2013-11-07 02:52:08 +00:00
|
|
|
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
|
|
|
|
IF( SDL_Init(SDL_INIT_VIDEO) < 0 ) EXIT(1);
|
|
|
|
|
|
|
|
SDL_WM_SetCaption("SDL Window - Multiplication Table", NULL);
|
|
|
|
|
|
|
|
screen = SDL_SetVideoMode( 400 , 300 , 32 , SDL_DOUBLEBUF|SDL_HWSURFACE|SDL_ANYFORMAT);
|
|
|
|
|
|
|
|
anim[0] = ImgLoader("./anim/icon.gif");
|
|
|
|
|
|
|
|
FPS_Initialize( );
|
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
DIM AS INT xi=1 , yi=1; // Use FOR display picture
|
2013-11-07 02:52:08 +00:00
|
|
|
|
|
|
|
WHILE (bRun) BEGIN
|
|
|
|
aTick++;
|
|
|
|
SDL_FillRect(screen , NULL , 0x221122);
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
IF ( aTick % 6 == 1) xi++;
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
IF ( xi > 9 ) THEN
|
2013-11-07 04:44:16 +00:00
|
|
|
xi = 1;
|
|
|
|
yi++;
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDIF
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
IF ( yi > 9 ) yi = 1;
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
FOR(INT i=0 TO i < yi STEP i++ ) BEGIN
|
2013-11-07 19:06:36 +00:00
|
|
|
animRect.x = 10 + i * 40;
|
2013-11-07 02:52:08 +00:00
|
|
|
animRect.y = 200;
|
|
|
|
SDL_BlitSurface( anim[0] , NULL , screen , &animRect );
|
|
|
|
END
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
FOR (INT i=0 TO i < xi STEP i++ ) BEGIN
|
2013-11-07 19:06:36 +00:00
|
|
|
animRect.x = 10 + i * 40;
|
2013-11-07 02:52:08 +00:00
|
|
|
animRect.y = 240;
|
|
|
|
SDL_BlitSurface( anim[0] , NULL , screen , &animRect );
|
|
|
|
END
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
// A Multiplication Table
|
|
|
|
FOR (INT i = 1 TO i <= 9 STEP i++) BEGIN
|
|
|
|
FOR( INT j = 1 TO j <= 9 STEP j++) BEGIN
|
|
|
|
txtIt("%2d" , i*j );
|
|
|
|
fColor = fColorW; // Default is white color
|
|
|
|
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 );
|
2013-11-07 19:06:36 +00:00
|
|
|
txtRect.x = 30 + i*30;
|
|
|
|
txtRect.y = 30 + j*15;
|
2013-11-07 02:52:08 +00:00
|
|
|
SDL_BlitSurface( imgTxt , NULL , screen , &txtRect );
|
2013-11-11 02:22:16 +00:00
|
|
|
END
|
|
|
|
END
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
txtIt("%1d %1d %1d %1d", bKeyUP , bKeyDOWN , bKeyLEFT , bKeyRIGHT);
|
|
|
|
fColor = fColorW; // Default is white color
|
|
|
|
imgTxt = TTF_RenderText_Solid( font , string , fColor );
|
|
|
|
SDL_BlitSurface( imgTxt , NULL , screen , NULL );
|
|
|
|
SDL_Flip(screen);
|
|
|
|
FPS_Fn();
|
|
|
|
|
|
|
|
WHILE ( SDL_PollEvent( &event ) ) BEGIN
|
|
|
|
SELECT ( event.type ){
|
|
|
|
CASE SDL_KEYDOWN:
|
2013-11-07 19:06:36 +00:00
|
|
|
SELECT ( event.key.keysym.sym ) BEGIN
|
2013-11-07 02:52:08 +00:00
|
|
|
CASE SDLK_UP:
|
|
|
|
bKeyUP = 1;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_DOWN:
|
|
|
|
bKeyDOWN = 1;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_LEFT:
|
|
|
|
bKeyLEFT = 1;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_RIGHT:
|
|
|
|
bKeyRIGHT = 1;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_ESCAPE:
|
|
|
|
bRun = 0;
|
|
|
|
ENDCASE
|
|
|
|
CASE_ELSE:
|
|
|
|
ENDCASE
|
2013-11-07 19:06:36 +00:00
|
|
|
ENDSELECT
|
2013-11-11 02:22:16 +00:00
|
|
|
ENDCASE
|
2013-11-07 19:06:36 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
CASE SDL_KEYUP:
|
|
|
|
SELECT ( event.key.keysym.sym ){
|
|
|
|
CASE SDLK_UP:
|
|
|
|
bKeyUP = 0;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_DOWN:
|
|
|
|
bKeyDOWN = 0;
|
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_LEFT:
|
2013-11-07 04:44:16 +00:00
|
|
|
bKeyLEFT = 0;
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDCASE
|
|
|
|
CASE SDLK_RIGHT:
|
2013-11-07 04:44:16 +00:00
|
|
|
bKeyRIGHT = 0;
|
2013-11-07 02:52:08 +00:00
|
|
|
ENDCASE
|
|
|
|
CASE_ELSE:
|
|
|
|
ENDCASE
|
2013-11-11 02:22:16 +00:00
|
|
|
ENDSELECT
|
2013-11-07 19:06:36 +00:00
|
|
|
ENDCASE
|
2013-11-11 02:22:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
CASE SDL_QUIT:
|
|
|
|
bRun = 0;
|
|
|
|
ENDCASE
|
2013-11-11 02:22:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
CASE_ELSE:
|
|
|
|
ENDCASE
|
2013-11-07 19:06:36 +00:00
|
|
|
ENDSELECT
|
2013-11-11 02:22:16 +00:00
|
|
|
WEND
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
// Deal with key states
|
|
|
|
IF (bKeyUP) animRect.y = animRect.y - dx;
|
|
|
|
IF (bKeyDOWN) animRect.y = animRect.y + dx;
|
|
|
|
IF (bKeyLEFT) animRect.x = animRect.x - dx;
|
|
|
|
IF (bKeyRIGHT) animRect.x = animRect.x + dx;
|
|
|
|
|
2013-11-07 19:06:36 +00:00
|
|
|
WEND
|
2013-11-07 04:44:16 +00:00
|
|
|
|
2013-11-07 02:52:08 +00:00
|
|
|
|
|
|
|
RETURN 0;
|
|
|
|
END
|