/* * catch.c * * Created on: 25.01.2020 * Author: victor */ #include "catch.h" SDL_Surface* screen; bool run; SDL_Rect sdl_rect, sdl_rect2; // key handler stuff bool keyPressed[SDLK_LAST]; // the images SDL_Surface* image_chargebar, *image_chargebar_dark, *image_cat, *image_bird; struct Cat cat; struct Bird bird[BIRD_COUNT]; int state; int main(int argc, char* argv[]) { memset(keyPressed,0,sizeof(keyPressed)); // initialize SDL if(SDL_Init(SDL_INIT_VIDEO) == -1) { printf("Could not initialize SDL! Error: %s", SDL_GetError()); return -1; } // create a window screen = SDL_SetVideoMode(640, 480, 8, SDL_DOUBLEBUF | SDL_FULLSCREEN); if(!screen) { printf("Could not create a SDL window! Error: %s", SDL_GetError()); return -1; } // initialize the font renderer Font_Init(); // load the images SDL_RWops* rwops = SDL_RWFromConstMem(chargebar_bmp, sizeof(chargebar_bmp) / sizeof(char)); image_chargebar = SDL_LoadBMP_RW(rwops,1); rwops = SDL_RWFromConstMem(chargebar_dark_bmp, sizeof(chargebar_dark_bmp) / sizeof(char)); image_chargebar_dark = SDL_LoadBMP_RW(rwops,1); rwops = SDL_RWFromConstMem(cat_bmp, sizeof(cat_bmp) / sizeof(char)); image_cat = SDL_LoadBMP_RW(rwops,1); SDL_SetColorKey(image_cat, SDL_SRCCOLORKEY, SDL_MapRGB(image_cat->format, 255, 0, 255)); // set the correct physics flags for the cat cat.jumping = true; // start the event loop run = true; while(run) { // process all sdl events SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: run = false; break; case SDL_KEYDOWN: keyPressed[event.key.keysym.sym] = true; break; case SDL_KEYUP: keyPressed[event.key.keysym.sym] = false; break; } } // quit if the esc key was pressed if(keyPressed[SDLK_ESCAPE]) run = false; // run the game routine gameRoutine(); // draw the sky sdl_rect.x = 0; sdl_rect.y = 0; sdl_rect.w = screen->w; sdl_rect.h = screen->h-FLOOR_HEIGHT; SDL_FillRect(screen,&sdl_rect,SDL_MapRGB(screen->format,0,0,200)); // draw the floor sdl_rect.y = sdl_rect.h; sdl_rect.h = FLOOR_HEIGHT; SDL_FillRect(screen,&sdl_rect,SDL_MapRGB(screen->format,0,200,000)); // draw the cat cat.x = screen->w/2 + image_cat->w/2; sdl_rect.x = cat.x; sdl_rect.y = cat.y; SDL_BlitSurface(image_cat, 0, screen, &sdl_rect); // draw the birds for(int i = 0; iscreen->h-FLOOR_HEIGHT-CAT_HEIGHT) { cat.y = screen->h-FLOOR_HEIGHT-CAT_HEIGHT; cat.jumping = false; cat.downwardForce = 0; } } else { //the player accumulated some energy and let go of the space bar if(!charge && cat.downwardForce<0) { cat.jumping = true; } else if(charge) { if(cat.downwardForce > -MAXIMUM_JUMP_FORCE) cat.downwardForce-=0.02; } } // }