1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include "engine.h"
#include "SDL/SDL_image.h" // add -lSDL_image to compile
#include "SDL/SDL_ttf.h" // add -lSDL_ttf
#include <sstream>
cProcess gamex;
struct Globs {
SDL_Surface *screen , *grass , *text , *text2 , *ntext;
SDL_Rect rscreen , rgrass , rtext , rtext2 , rntext;
SDL_Event event;
TTF_Font *font;
bool gameover;
Globs () {
gameover = false;
}
} glob;
struct fpsGlob {
int tick;
int runtick;
fpsGlob() {
tick = 0;
runtick = 0;
}
} fpsglob;
SDL_Surface *Image ( std::string file ) {
SDL_Surface *temp = NULL, *image = NULL;
temp = IMG_Load ( file.c_str() );
if ( temp != NULL ) {
image = SDL_DisplayFormat ( temp );
SDL_FreeSurface ( temp );
}
return image;
}
SDL_Rect Rect( int x, int y ) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
return rect;
}
SDL_Rect Rect( int x, int y , int w , int h ) {
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.h = h;
rect.w = w;
return rect;
}
// change an int to string
std::string itos( int n ) {
std::ostringstream itos;
itos.str("");
itos << n;
return itos.str();
}
SDL_Surface *rendertext ( char x[] ) {
SDL_Surface *rtext;
SDL_Color fcolor = { 255 ,255 ,255 };
rtext = TTF_RenderText_Solid(glob.font , x , fcolor);
return rtext;
}
void build () {
char txt[] = "-1";
SDL_Init ( SDL_INIT_EVERYTHING );
TTF_Init();
glob.screen = SDL_SetVideoMode ( 800 , 600 , 32 , SDL_SWSURFACE );
//if ( screen == NULL ) { return 1; }
SDL_WM_SetCaption ( "Sdl Test", NULL );
glob.font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 16);
if (glob.font == NULL) { printf ("\nbadfont\n"); }
glob.rtext = Rect(50,300);
glob.rtext2 = Rect(50,330);
glob.rntext = Rect(50,360);
glob.rgrass = Rect(50,50);
glob.rscreen = Rect(0,0,800,600);
glob.grass = Image( "grass.bmp" );
glob.text = rendertext ( txt );
glob.text2 = rendertext ( txt );
glob.ntext = rendertext ( txt );
}
void gameloop( Purpose P ) {
switch (P) {
case pStop:
case pStart: {
if ( glob.gameover == false ) {
if (SDL_PollEvent ( &glob.event ) ) {
switch ( glob.event.type ) {
case SDL_QUIT:
glob.gameover = true;
break;
case SDL_KEYDOWN:
if ( glob.event.key.keysym.sym == SDLK_ESCAPE ||
glob.event.key.keysym.sym == SDLK_q) {
glob.gameover = true;
break;
}
break;
}
}
//blit to screen here
SDL_FillRect ( glob.screen , &glob.rscreen , 0 );
SDL_BlitSurface( glob.grass , NULL , glob.screen , &glob.rgrass );
SDL_BlitSurface( glob.text , NULL , glob.screen , &glob.rtext );
SDL_BlitSurface( glob.text2 , NULL , glob.screen , &glob.rtext2 );
SDL_BlitSurface( glob.ntext , NULL , glob.screen , &glob.rntext );
SDL_Flip ( glob.screen ) ;
}
break;
}
case pSleep: {
printf (" error ");
if (P == pSleep) { printf(" sleep "); }
break;
}
}
}
void fps( Purpose P ) {
switch (P) {
case pStop: {
printf ( "%d\n",fpsglob.tick);
break;
}
case pStart: {
fpsglob.runtick++;
std::string tex;
char txt[10];
tex = itos ( fpsglob.tick );
strcpy(txt , tex.c_str());
glob.text = rendertext ( txt );
tex = itos ( fpsglob.runtick );
strcpy(txt , tex.c_str());
glob.text2 = rendertext ( txt );
tex = itos ( gamex.nT("FPS") );
strcpy(txt , tex.c_str());
glob.ntext = rendertext ( txt );
fpsglob.tick = 0;
break;
}
case pSleep: {
fpsglob.tick++;
break;
}
}
}
// game start
int main ( int argc , char* args[] ) {
build();
gamex.Add( gameloop, "Main" );
gamex.Add( fps , "FPS" );
gamex.Settick ( "FPS" , 1000 , 1000 );
//game loop
while ( !glob.gameover ) {
enginecontrol ( gamex );
}
gamex.Stop ( "FPS" );
// clean up
SDL_FreeSurface ( glob.screen );
SDL_FreeSurface ( glob.grass );
SDL_FreeSurface ( glob.text );
TTF_CloseFont( glob.font );
TTF_Quit();
SDL_Quit();
return 0;
}
|