edit: fixed my own stupidly broken C++ headers
edit2: you should use proper indentation
edit3: your frame rate limiting code is broken - game updates as fast as SDL can draw the box. I fixed this and a couple other minor things:
#include "iostream"
#include "stdlib.h"
#include "SDL/SDL.h"
using namespace std;
int main(){
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE);
//SDL_Surface* dis=SDL_SetVideoMode(200,200,24,SDL_SWSURFACE|SDL_NOFRAME);
SDL_Event game;
SDL_Rect playersprite,background;
Uint8* key;
int quit=0;
Uint8 r=255,g=255,b=255;
int currentTime,oldTime,framerate = 15, framemillisec;
cout<<framemillisec<<", "<<currentTime<<", "<<oldTime<<endl;
int xp=80,yp=80;
int change_color=0,change_color_pressed=0;
//set the dimensions of the playersprite and backgorund window rectangle
background.x=0; background.y=0;
background.w=200; background.h=200;
playersprite.x=0; playersprite.y=0;
playersprite.w=40; playersprite.h=40;
//seed random number generator
srand(currentTime);
framemillisec = 1000/framerate; // calculate frame delay in milleseconds based on desired framerate
oldTime=SDL_GetTicks(); //initialize first timestamp for framerate calculation
while (quit==0){
// while (SDL_PollEvent(&game)){}
SDL_PollEvent(&game);
key=SDL_GetKeyState(NULL);
if(key[sDLK_q]){quit=1;}
if(key[sDLK_a]){change_color=1;}else{change_color_pressed=0;}
if(key[sDLK_UP]){yp-=2;}
if(key[sDLK_DOWN]){yp+=2;}
if(key[sDLK_LEFT]){xp-=2;}
if(key[sDLK_RIGHT]){xp+=2;}
if(xp<0){xp=0;}
if(xp>200-40){xp=200-40;}
if(yp<0){yp=0;}
if(yp>200-40){yp=200-40;}
playersprite.x=xp; playersprite.y=yp;
if(change_color==1&&change_color_pressed==0){
r=rand()%255; g=rand()%255; b=rand()%255;
cout<<"R = "<<(int)r<<", G = "<<(int)g<<", B = "<<(int)b<<endl;
change_color_pressed=1;
}
change_color=0;
SDL_FillRect(dis,&background,SDL_MapRGB(dis->format,0,0,0));// CLEAN THE SCREEN.
SDL_FillRect(dis,&playersprite,SDL_MapRGB(dis->format,r,g, B) ); // Draw player rectangle
SDL_Flip(dis);
// Pause execution until next frame is finished
currentTime=SDL_GetTicks();
// cout<<framemillisec-(currentTime-oldTime)<<", "<<currentTime<<", "<<oldTime<<endl;
if(currentTime-oldTime<framemillisec) {
SDL_Delay(framemillisec-(currentTime-oldTime));
} else
cout<<"game too slow for desired framerate"<<endl;
oldTime=SDL_GetTicks();
}
SDL_Quit();
return 0;
}
game.zip