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
|
//---------------- includes ----------------
#include "global.h"
//---------------- global variables ----------------
//"engine"
Uint8 *keystates;
SDL_Surface *screen; //the screen (main sdl surface which is visible on the monitor)
//sprites
gfxSprite spr_t[7];
gfxSprite spr_player[2];
gfxSprite spr_player2[2];
gfxSprite spr_background;
gfxFont font;
//game objects
CPlayer player;
CPlayer player2;
CMap map;
CTile tileset[9];
//scroll offset
int scroll_x = 0;
int scroll_y = 0;
//---------------- main ----------------
int main(int argc, char *argv[]){
unsigned int framestart;
float fps = 0, framefps = 0;
int divisor;
bool done;
SDL_Event event;
//---------------- init the "engine" ----------------
//initialize SDL
gfx_init(640,480, false);
//get keystate array
keystates = SDL_GetKeyState(0);
//---------------- load resources (graphics) ----------------
spr_t[0].init("gfx/t1.bmp"); //tile graphics
spr_t[1].init("gfx/t2.bmp");
spr_t[2].init("gfx/t3.bmp", 255, 0, 255);
spr_t[3].init("gfx/tsloper.bmp", 255, 0, 255);
spr_t[4].init("gfx/tslopel.bmp", 255, 0, 255);
spr_t[5].init("gfx/t6.bmp", 255, 0, 255);
spr_t[6].init("gfx/t7.bmp", 255, 0, 255);
spr_player[0].init("gfx/left2.bmp", 255,0,255); //player graphics
spr_player[1].init("gfx/right2.bmp", 255,0,255);
spr_player2[0].init("gfx/Rleft.bmp", 255,0,255); //player graphics
spr_player2[1].init("gfx/Rright.bmp", 255,0,255);
spr_background.init("gfx/bg.bmp"); //background
font.init("gfx/font0.bmp"); //font
//---------------- init the game variables ----------------
tileset[0].type = t_nonsolid; tileset[0].spr = NULL;
tileset[1].type = t_solid; tileset[1].spr = NULL;
tileset[2].type = t_solid; tileset[2].spr = &spr_t[0];
tileset[3].type = t_solid; tileset[3].spr = &spr_t[1];
tileset[4].type = t_nonsolid; tileset[4].spr = &spr_t[2];
tileset[5].type = t_sloperight; tileset[5].spr = &spr_t[3];
tileset[6].type = t_slopeleft; tileset[6].spr = &spr_t[4];
tileset[7].type = t_solid; tileset[7].spr = &spr_t[5];
tileset[8].type = t_solid; tileset[8].spr = &spr_t[6];
//initialize the map, the player has already been initialized by its constructor
map.loadMap("maps/map01.map");
printf("\nhere comes the game loop...\n") ;
done = false;
//---------------- game loop ----------------
while (!done){
framestart = SDL_GetTicks();
//handle messages
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
done = true;
break;
default:
break;
}
}
if(keystates[SDLK_ESCAPE]) //quit?
done = true;
//---------------- update objects (game logic) ----------------
player.think();
player2.think2();
//---------------- draw everything (render the scene) ----------------
int sdx = (scroll_x%spr_background.getWidth());
int sdy = (scroll_y%spr_background.getHeight());
spr_background.draw(-sdx, - sdy);
spr_background.draw(spr_background.getWidth() - sdx, -sdy);
spr_background.draw(- sdx, spr_background.getHeight()-sdy);
spr_background.draw(spr_background.getWidth() - sdx, spr_background.getHeight()-sdy);
map.draw();
player.draw();
player2.draw();
//the info text
font.drawf(0,0, "fps: frame/real/lock: %.1f/%.1f/%.1f", framefps, fps, (float)(1000 / WAITTIME));
//---------------- that's it, now flip the buffers and keep the framerate constant ----------------
divisor = SDL_GetTicks()-framestart;
if(divisor != 0)
framefps = (float)( 1000 / divisor ); //this is the framerate without vsync and the frame break
else
fps = 1111.11f;
SDL_Flip(screen); //double buffering -> flip buffers, also waits for vsync
while((SDL_GetTicks()-framestart) < WAITTIME); //framebreak - keep framerate constant at 1000/WAITTIME fps
divisor = SDL_GetTicks()-framestart;
if(divisor != 0)
fps = (float)( 1000 / (SDL_GetTicks()-framestart) ); //this is the framerate with vsync and the framebreak (should be equal to 1000/WAITTIME)
else
fps = 1111.11f;
}
printf("\n\nthat's all folks :)\n");
return 0;
}
| |