Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sdl_szablon.cpp : Akademia ETI
- // Krzysztof Bruniecki 2018
- #include <iostream>
- #include <sdl.h>
- #undef main
- /**
- * Loads a BMP image into a texture on the rendering device
- * @param file The BMP image file to load
- * @param ren The renderer to load the texture onto
- * @return the loaded texture, or nullptr if something went wrong.
- */
- SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren) {
- //Initialize to nullptr to avoid dangling pointer issues
- SDL_Texture *texture = nullptr;
- //Load the image
- SDL_Surface *loadedImage = SDL_LoadBMP(file.c_str());
- //If the loading went ok, convert to texture and return the texture
- if (loadedImage != nullptr) {
- texture = SDL_CreateTextureFromSurface(ren, loadedImage);
- SDL_FreeSurface(loadedImage);
- //Make sure converting went ok too
- if (texture == nullptr) {
- //logSDLError(std::cout, "CreateTextureFromSurface");
- }
- }
- else {
- //logSDLError(std::cout, "LoadBMP");
- }
- return texture;
- }
- /**
- * Draw an SDL_Texture to an SDL_Renderer at position x, y, preserving
- * the texture's width and height
- * @param tex The source texture we want to draw
- * @param ren The renderer we want to draw to
- * @param x The x coordinate to draw to
- * @param y The y coordinate to draw to
- */
- void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y) {
- //Setup the destination rectangle to be at the position we want
- SDL_Rect dst;
- dst.x = x;
- dst.y = y;
- //Query the texture to get its width and height to use
- SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);
- SDL_RenderCopy(ren, tex, NULL, &dst);
- }
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 480
- struct Pocisk {
- int x;
- int y;
- int dx;
- int dy;
- SDL_Texture *tex;
- void init() {
- x = -100;
- y = -100;
- dx = 0;
- dy = 0;
- }
- void update() {
- x += dx;
- y += dy;
- }
- };
- int main()
- {
- #pragma region Inicjalizacja biblioteki SDL
- if (SDL_Init(SDL_INIT_VIDEO) != 0) {
- std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
- return 1;
- }
- SDL_Window *win = SDL_CreateWindow("Akademia ETI!", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if (win == nullptr) {
- std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
- SDL_Quit();
- return 1;
- }
- SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if (ren == nullptr) {
- SDL_DestroyWindow(win);
- std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
- SDL_Quit();
- return 1;
- }
- #pragma endregion
- int cState = 2;
- std::string backFn = "mapa.bmp";
- std::string playerFn = "c0.bmp";
- SDL_Texture * cTex[4];
- Pocisk p;
- p.init();
- p.tex = loadTexture("bomba.bmp", ren);
- SDL_Texture *backTex = loadTexture(backFn, ren);
- cTex[0] = loadTexture(playerFn, ren);
- cTex[1] = loadTexture("c1.bmp", ren);
- cTex[2] = loadTexture("c2.bmp", ren);
- cTex[3] = loadTexture("c3.bmp", ren);
- SDL_Event e;
- bool quit = false;
- int dx = 0;
- int dy = 0;
- int dx2 = 0;
- int dy2 = 0;
- int x = SCREEN_WIDTH / 2;
- int y = SCREEN_HEIGHT / 2;
- int x2=x, y2=y;
- while (!quit) {
- while (SDL_PollEvent(&e)) {
- if (e.type == SDL_QUIT) {
- quit = true;
- }
- if (e.type == SDL_KEYDOWN) {
- switch (e.key.keysym.sym) {
- case SDLK_DOWN:
- dx = 0;
- dy = 1;
- break;
- case SDLK_LEFT:
- dx = -1;
- dy = 0;
- break;
- case SDLK_RIGHT:
- dx = 1;
- dy = 0;
- break;
- case SDLK_UP:
- dx = 0;
- dy = -1;
- break;
- case SDLK_a:
- dx = -4;
- dy2 = 0;
- break;
- case SDLK_SPACE:
- p.x = x;
- p.y = y;
- p.dx = 2 * dx;
- p.dy = 2 * dy;
- break;
- case SDLK_ESCAPE:
- quit = true;
- break;
- default:
- break;
- }
- }
- if (e.type == SDL_MOUSEBUTTONDOWN) {
- quit = true;
- }
- }
- //Renderujemy cala scene
- //
- //TODO :blokada wyjscia za ekran
- //
- /////////////1
- x += dx;
- y += dy;
- p.update();
- if (dy < 0) cState = 0;
- else if(dy>0) cState = 2;
- else if (dx < 0) cState = 3;
- else if (dx>0) cState = 1;
- if (x <= 0) { x = 0; dx = 1; }
- /////////////2
- x2 += dx2;
- y2 += dy2;
- SDL_RenderClear(ren);
- renderTexture(backTex, ren, -x, -y);
- renderTexture(cTex[cState], ren, x, y);
- renderTexture(p.tex, ren, p.x, p.y);
- //renderTexture(cTex[1], ren, x2, y2);
- SDL_RenderPresent(ren);
- }
- SDL_DestroyTexture(backTex);
- SDL_DestroyTexture(cTex[0]);
- SDL_DestroyTexture(cTex[1]);
- SDL_DestroyTexture(cTex[2]);
- SDL_DestroyTexture(cTex[3]);
- SDL_DestroyRenderer(ren);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement