#include "util.h" #include #include #include using namespace std; Sint16 cx=320, cy=240, cr=7; SDL_Surface *guy; void drawScene(SDL_Surface *screen) { lock(screen); for(int x=0;x<640;x++) for(int y=0;y<480;y++) pixelColor(screen, x,y, 0x996633ff ^ ((x+y) << 8) ); /* cr = clamp(cr, 4, 50, 1); cx = clamp(cx, 0, 639, 4); cy = clamp(cy, 0, 479, 4); */ SDL_Rect destRect = guy->clip_rect; destRect.x = cx - destRect.w/2; destRect.y = cy - destRect.h/2; SDL_BlitSurface(guy,NULL, screen,&destRect); filledCircleRGBA(screen, cx,cy,cr, 0,255,255, (51-cr)*4+67); unlock(screen); SDL_Flip(screen); } void pollKeys( Uint8 *keys ) { if ( keys[SDLK_UP] ) cy = (cy-1 < 0) ? 0 : cy-1; if ( keys[SDLK_DOWN] ) cy = (cy+1 > 479) ? 479 : cy+1; if ( keys[SDLK_LEFT] ) cx = (cx-1 < 0) ? 0 : cx-1; if ( keys[SDLK_RIGHT] ) cx = (cx+1 > 639) ? 639 : cx+1; if ( keys[SDLK_KP_MINUS] ) cr = (cr-1 < 0) ? 0 : cr-1; if ( keys[SDLK_KP_PLUS] ) cr = (cr+1 > 50) ? 50 : cr+1; } SDL_Surface *init() { // do inits //if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) <0 ) if( SDL_Init(SDL_INIT_VIDEO) <0 ) { cout << "Unable to init SDL: " << SDL_GetError() << endl; return NULL; } else cout << "Successful init" << endl; initRand(); atexit(SDL_Quit); // listens for quit events and cleans up SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL ); guy = IMG_Load("sprites/guy.png"); if ( screen == NULL ) cout << "Screen is null: " << SDL_GetError() << endl; if ( guy == NULL ) { cout << "Image is null: " << IMG_GetError() << endl; return NULL; } return screen; } int main() { SDL_Event event; Uint32 start,end; bool done = false; SDL_Surface *screen = init(); if (screen == NULL) return 1; while (!done) { // TODO timer mark start = SDL_GetTicks(); //cout << "start: " << start << endl; while (!done && SDL_PollEvent(&event)) { //cout << "ev: at " << SDL_GetTicks() << endl; switch (event.type) { case SDL_QUIT: done = true; break; default: break; } break; } if (done) break; pollKeys( SDL_GetKeyState(NULL) ); drawScene(screen); // TODO timer check; delay for (25ms - usage) // only want 40 fps max end = SDL_GetTicks(); //cout << "end: " << end << endl; // if (end-start < 20) // { // cout << "Scene drawn, delaying for " << 20+start-end << " ms" << endl; // SDL_Delay(20+start-end); // } SDL_Delay(20); } }