From 3e48a47b9c47721e041b5b54a55ce5cb74d27ddb Mon Sep 17 00:00:00 2001 From: miguel Date: Tue, 12 Sep 2017 21:12:41 +0200 Subject: first commit of boban on the run --- Makefile | 11 +++ World.cpp | 23 ++++++ World.h | 48 +++++++++++ earth01.bmp | Bin 0 -> 1322 bytes fonts.bmp | Bin 0 -> 35338 bytes gridder01.bmp | Bin 0 -> 1322 bytes gridder02.bmp | Bin 0 -> 1322 bytes guy01.bmp | Bin 0 -> 480122 bytes guy02.bmp | Bin 0 -> 640138 bytes hello.bmp | Bin 0 -> 384122 bytes main.cpp | 253 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 335 insertions(+) create mode 100644 Makefile create mode 100644 World.cpp create mode 100644 World.h create mode 100644 earth01.bmp create mode 100644 fonts.bmp create mode 100644 gridder01.bmp create mode 100644 gridder02.bmp create mode 100644 guy01.bmp create mode 100644 guy02.bmp create mode 100644 hello.bmp create mode 100644 main.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a1445b2 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +CPPFLAGS = $(shell sdl2-config --cflags --libs) + +run: game + ./game + +game: World.o main.o + $(CXX) -o $@ $(CPPFLAGS) $^ + +clean: + rm -f game + rm -f *.o diff --git a/World.cpp b/World.cpp new file mode 100644 index 0000000..66b1aba --- /dev/null +++ b/World.cpp @@ -0,0 +1,23 @@ +#include "World.h" + + +World::World(int level) +{ + player.speed=1; + player.x2=player.x=0; + player.anim=0; + // generate level + player.y=bricks[0][0].altitude+1; +} + +void World::sim(double time) +{ + player.x2+=time/1000.0*player.speed; + player.anim=(int)(player.x2*5)%2; + while(player.x2>1.0) + { + player.x2-=1; + player.x+=1; + } +} + diff --git a/World.h b/World.h new file mode 100644 index 0000000..fbd5279 --- /dev/null +++ b/World.h @@ -0,0 +1,48 @@ +#include +#include + +struct Brick +{ + int altitude; //meter above sea level + int type; +// ~Brick(){std::cout<<"deconstruct"<< std::endl;} +// Brick(int altitude, int type){std::cout<<"construct"<< std::endl;} + + +}; + +struct Player +{ + int x; + int y; + double x2; + int speed; + int anim; +}; + +struct World +{ + Player player; + std::vector> bricks= + { + { {0,0} }, + { {0,0} }, + { {0,0} }, + { {0,0},{5,1} }, + { {0,0},{5,1} }, + { {0,0},{5,1} }, + { {0,0},{5,1} }, + {}, + { {5,1} }, + { {5,1} }, + { {0,0},{5,1} }, + { {0,0},{5,1} }, + { {0,0},{5,1} }, + { {1,0},{5,1} }, + { {2,0},{5,1},{7,1} }, + { {3,0},{6,1} } + }; + + World(int level); + void sim(double); +}; diff --git a/earth01.bmp b/earth01.bmp new file mode 100644 index 0000000..a9a10ce Binary files /dev/null and b/earth01.bmp differ diff --git a/fonts.bmp b/fonts.bmp new file mode 100644 index 0000000..9235c8c Binary files /dev/null and b/fonts.bmp differ diff --git a/gridder01.bmp b/gridder01.bmp new file mode 100644 index 0000000..a714c93 Binary files /dev/null and b/gridder01.bmp differ diff --git a/gridder02.bmp b/gridder02.bmp new file mode 100644 index 0000000..35a145a Binary files /dev/null and b/gridder02.bmp differ diff --git a/guy01.bmp b/guy01.bmp new file mode 100644 index 0000000..169f872 Binary files /dev/null and b/guy01.bmp differ diff --git a/guy02.bmp b/guy02.bmp new file mode 100644 index 0000000..5e89c77 Binary files /dev/null and b/guy02.bmp differ diff --git a/hello.bmp b/hello.bmp new file mode 100644 index 0000000..5c2e250 Binary files /dev/null and b/hello.bmp differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..303e656 --- /dev/null +++ b/main.cpp @@ -0,0 +1,253 @@ +#include + +#include "World.h" + +using SDLInitType=std::pair>; + +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) != 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; +} + +// + +int main(int, char**){ + + // initial values + int win_width=640; + int win_height=480; + + 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; + + 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("hello.bmp",0,0,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)); + + // SDL_SetTextureColorMod(tex4,255,0,0); + + bool quit = false; + SDL_Event event; + + Uint32 frameTime; + Uint32 lastFrameTime = 0; + Uint32 deltaTime = 0; + Uint32 fpsTime = 0; + Uint32 fpsCount = 0; + + Uint32 mouse_x=0; + Uint32 mouse_y=0; + + + // 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>3000) + { + std::cout << "FPS : " << fpsCount*1000/fpsTime << std::endl; + 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.bricks[(mouse_x+50-world.player.x2)/100+world.player.x-5][0].altitude=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; + } + } + + // rendering & run world simu + SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); + SDL_RenderClear(ren); + + int br=-1; + + for(int i=world.player.x-5;i=world.bricks.size())continue; + + for(int j=0;j