#include #include #include "World.h" using SDLInitType=std::pair>; // audio from http://www.brainybetty.com/soundsforpowerpoint2.htm // https://freesound.org/people/ProjectsU012/sounds/341695/ void sdl_play_sound() { static bool first=true; static SDL_AudioSpec wavSpec; static Uint32 wavLength; static Uint8 *wavBuffer; static SDL_AudioDeviceID deviceId; if (first) { SDL_LoadWAV("coin.wav", &wavSpec, &wavBuffer, &wavLength); deviceId = SDL_OpenAudioDevice(NULL, 0, &wavSpec, NULL, 0); SDL_PauseAudioDevice(deviceId, 0); first=false; } // SDL_LoadWAV("died.wav", &wavSpec, &wavBuffer, &wavLength); // SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength); // SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength); int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); //SDL_CloseAudioDevice(deviceId); //SDL_FreeWAV(wavBuffer); } SDLInitType sdl_start(int win_width, int win_height) { //First we need to start up SDL, and make sure it went ok if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0){ std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; return {1,{nullptr,nullptr}}; } std::cout << "SDL_Init OK" << std::endl; atexit(SDL_Quit); //Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it SDL_Window *win = SDL_CreateWindow("Hello World!", 200, 100, win_width, win_height, SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE); //Make sure creating our window went ok if (win == nullptr){ std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return {1,{nullptr,nullptr}}; } std::cout << "SDL_CreateWindow OK" << std::endl; //Create a renderer that will draw to the window, -1 specifies that we want to load whichever //video driver supports the flags we're passing //Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering //SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be //synchronized with the monitor's refresh rate SDL_Renderer *ren = SDL_CreateRenderer(win, -1,0);//, SDL_RENDERER_SOFTWARE);// | SDL_RENDERER_PRESENTVSYNC|SDL_HINT_RENDER_VSYNC); if (ren == nullptr){ SDL_DestroyWindow(win); std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; return {1,{win,nullptr}}; } std::cout << "SDL_CreateRenderer OK" << std::endl; return {0,{win,ren}}; } void sdl_stop(SDLInitType sdl_init) { if(sdl_init.second.second!=nullptr)SDL_DestroyRenderer(sdl_init.second.second); if(sdl_init.second.first!=nullptr)SDL_DestroyWindow(sdl_init.second.first); } SDL_Texture* sdl_load_texture(std::string imagePath,int key_r, int key_g, int key_b,SDL_Renderer *ren) { //SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface //this lets us choose when to upload or remove textures from the GPU SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str()); SDL_SetColorKey(bmp, SDL_TRUE, SDL_MapRGB(bmp->format, key_r, key_g, key_b)); if (bmp == nullptr){ std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl; return nullptr; } std::cout << "SDL_LoadBMP ("<pixels,SDL_MapRGBA(bmp4->format, 255, 0, 255,220), bmp4->h * bmp4->pitch); // SDL_UnlockSurface(bmp4); //To use a hardware accelerated texture for rendering we can create one from //the surface we loaded return tex; } void sdl_put_letter(SDL_Renderer *ren, SDL_Texture *tex,int x,int y, int w, int h, int r, int g, int b, int ch) { if(ch>=123&&ch<=126)ch-=123-40; else if(ch>=97&&ch<=122)ch-=97; else if(ch>=65&&ch<=96)ch-=65; else if(ch>=32&&ch<=63)ch-=32-50; else ch=81; int col=ch%10; int row=ch/10; SDL_SetTextureColorMod(tex,r,g,b); SDL_Rect src={col*8,row*10,8,10}; SDL_Rect dst={x,y,w,h}; SDL_RenderCopy(ren,tex,&src,&dst); } void sdl_put_str(SDL_Renderer *ren, SDL_Texture *tex,int x, int y,int size,std::string str,int r,int g, int b) { int pos=0; for(char ch:str) { pos++; sdl_put_letter(ren,tex,x+8*size*pos,y, 8*size,10*size, r,g,b, ch); } } // int main(int, char**){ // initial window size int win_width=640; int win_height=480; // some other vars bool quit = false; SDL_Event event; Uint32 frameTime; Uint32 lastFrameTime = 0; Uint32 deltaTime = 0; Uint32 fpsTime = 0; Uint32 fpsCount = 0; Uint32 fps = 0; Uint32 mouse_x=0; Uint32 mouse_y=0; // init SDL and assign SDL_Renderer on success SDLInitType sdl_init=sdl_start(win_width,win_height); if(sdl_init.first!=0) { sdl_stop(sdl_init); return sdl_init.first; } SDL_Renderer *ren=sdl_init.second.second; // init Textures std::vector textures; textures.push_back(sdl_load_texture("guy01.bmp",0,0,255,ren)); textures.push_back(sdl_load_texture("guy02.bmp",0,0,255,ren)); textures.push_back(sdl_load_texture("coin.bmp",255,255,255,ren)); textures.push_back(sdl_load_texture("fonts.bmp",0,0,0,ren)); textures.push_back(sdl_load_texture("earth01.bmp",0,0,0,ren)); textures.push_back(sdl_load_texture("gridder01.bmp",255,255,255,ren)); textures.push_back(sdl_load_texture("gridder02.bmp",255,255,255,ren)); // init world LEVEL 1 World world(1); while (!quit) { // calc delta and FPS frameTime = SDL_GetTicks(); deltaTime = frameTime - lastFrameTime; lastFrameTime = frameTime; fpsTime+=deltaTime; fpsCount++; if(fpsTime>1000) { fps=fpsCount*1000/fpsTime; fpsTime=0; fpsCount=0; } // handle events (mouse & kb & win resize) while(!quit && SDL_PollEvent(&event)) { if (event.type == SDL_WINDOWEVENT && (event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)) { // SDL_Log("Window %d size changed to %dx%d", // event.window.windowID, event.window.data1, //event.window.data2); win_width=event.window.data1; win_height=event.window.data2; } if (event.type==SDL_MOUSEBUTTONDOWN) { mouse_x=event.button.x; mouse_y=event.button.y; world.mouseclick((mouse_x+50-world.player.x2)/100+world.player.x-5,10-mouse_y/100); } switch (event.type) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) quit = true; break; default: break; } } // clear screen SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); SDL_RenderClear(ren); // render visible area for(int i=world.player.x-5;i=world.bricks.size())continue; SDL_Rect rect={br*100-100*world.player.x2,(10-world.coins_pos[i])*100,100,100}; SDL_RenderCopy(ren,textures[2],NULL,&rect); for(int j=0;j