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
|
#include <SDL.h>
#include <string>
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
//Creating the screen and the image
SDL_Surface *screen;
SDL_Surface *image;
image = SDL_LoadBMP("Test.bmp");
screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Simple Game", NULL);//Keep in main
bool running = true;
const int FPS = 30;
Uint32 start;
//Create Rectangle
SDL_Rect rect;
rect.x = 40;
rect.y = 40;
rect.w = 32;
rect.h = 32;
Uint8 *keyStates = SDL_GetKeyState(0);
Uint32 RectColor = SDL_MapRGB(screen->format, 234, 0, 45);
Uint32 ScreenColor = SDL_MapRGB(screen->format, 25, 23, 90);
//Offsetting the bmp image
SDL_Rect offset;
offset.x = 0;
offset.y = 570;
int gravity = 1;
while(running)//Keep this and everything in it in main
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
running = false;
break;
}
}
if(keyStates[SDLK_w])
{
//offset.y -= 1;
}
if(keyStates[SDLK_a])
{
offset.x -= 1;
}
if(keyStates[SDLK_s])
{
//offset.y += 1;
}
if(keyStates[SDLK_d])
{
offset.x += 1;
}
if((keyStates[SDLK_s]) && (offset.y == 570))
{
offset.y -= 1;
}
if((keyStates[SDLK_d]) && (offset.x == 770))
{
offset.x -= 1;
}
if(offset.y == rect.y)
{
offset.x -= 1;
}
if(keyStates[SDLK_SPACE])
{
offset.y -= 2;
}
if(offset.y < 570)
{
offset.y += gravity;
}
if(offset.y < 500)
{
offset.y += gravity;
keyStates[SDLK_SPACE] = SDL_RELEASED;
}
//We are blitting image, NULL, telling what we are blitting image to
SDL_FillRect(screen, NULL, ScreenColor);
SDL_FillRect(screen, &rect, RectColor);
SDL_BlitSurface(image, NULL, screen, &offset);
SDL_Flip(screen);
if(1000/FPS>SDL_GetTicks()-start)
{
SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
}
}
SDL_FreeSurface(image);//Keep in main
SDL_Quit();// Keep in main
}
|