diff --git a/testSDL/lesson3/-.- b/testSDL/lesson3/-.- index 335e0d5..7b37462 100755 Binary files a/testSDL/lesson3/-.- and b/testSDL/lesson3/-.- differ diff --git a/testSDL/lesson3/kirill.jpg b/testSDL/lesson3/kirill.jpg new file mode 100644 index 0000000..7727f38 Binary files /dev/null and b/testSDL/lesson3/kirill.jpg differ diff --git a/testSDL/lesson3/kirill_wings.png b/testSDL/lesson3/kirill_wings.png new file mode 100644 index 0000000..2e3fad7 Binary files /dev/null and b/testSDL/lesson3/kirill_wings.png differ diff --git a/testSDL/lesson3/main.cpp b/testSDL/lesson3/main.cpp index 2ee40b1..530deca 100644 --- a/testSDL/lesson3/main.cpp +++ b/testSDL/lesson3/main.cpp @@ -2,23 +2,6 @@ #include #include -SDL_Renderer* ren; - -SDL_Texture* LoadImage(const char* path) -{ - SDL_Surface* loadedImage = NULL; - SDL_Texture* texture = NULL; - loadedImage = SDL_LoadBMP(path); - if (loadedImage != 0) - { - texture = SDL_CreateTextureFromSurface(ren, loadedImage); - SDL_FreeSurface(loadedImage); - } - else - std::cout << SDL_GetError() << std::endl; - return texture; -} - void move_UP (SDL_Renderer* ren, SDL_Texture* tex, SDL_Rect &destrect, int offset = 5) { destrect.y -= offset; @@ -54,6 +37,8 @@ void render_UPDATE (SDL_Renderer* ren, SDL_Texture* tex[], SDL_Rect* destrect[], SDL_RenderCopy(ren, tex[0], NULL, destrect[0]); if (states[1]) SDL_RenderCopy(ren, tex[1], NULL, destrect[1]); + if (states[2]) + SDL_RenderCopy(ren, tex[2], NULL, destrect[2]); } int main() @@ -65,13 +50,14 @@ int main() return 1; } int request = SDL_GetDesktopDisplayMode(0,&displayMode); + std::cout << displayMode.w << " " << displayMode.h << std::endl; SDL_Window* win = SDL_CreateWindow("Hello World!", 0, 0, 1280, 720, SDL_WINDOW_SHOWN); if (win == nullptr) { std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; return 1; } - ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + SDL_Renderer* ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (ren == nullptr) { std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; @@ -80,21 +66,28 @@ int main() const int player_wight = 333; const int player_height = 227; double texture_scale = 1.0; - SDL_Rect player_rect; - player_rect.x = 0; - player_rect.y = 0; - player_rect.w = player_wight; - player_rect.h = player_height; + SDL_Rect sanya_rect; + sanya_rect.x = 0; + sanya_rect.y = 0; + sanya_rect.w = player_wight; + sanya_rect.h = player_height; + SDL_Rect kirill_rect; + kirill_rect.x = 0; + kirill_rect.y = 200; + kirill_rect.w = player_wight; + kirill_rect.h = player_height; SDL_Rect background_rect; background_rect.x = 0; background_rect.y = 0; background_rect.h = displayMode.h; background_rect.w = displayMode.w; - SDL_Texture* player = IMG_LoadTexture(ren, "player1.png"); + SDL_Texture* sanya = IMG_LoadTexture(ren, "sanya_wings.png"); + SDL_Texture* kirill = IMG_LoadTexture(ren, "kirill_wings.png"); SDL_Texture* background = IMG_LoadTexture(ren, "background.bmp"); SDL_RenderClear(ren); SDL_RenderCopy(ren, background, NULL, &background_rect); - SDL_RenderCopy(ren, player, NULL, &player_rect); + SDL_RenderCopy(ren, sanya, NULL, &sanya_rect); + SDL_RenderCopy(ren, kirill, NULL, &kirill_rect); SDL_RenderPresent(ren); int bW, bH; SDL_QueryTexture(background, NULL, NULL, &bW, &bH); @@ -102,57 +95,96 @@ int main() SDL_Event event; const Uint8 *keyboardState = SDL_GetKeyboardState(NULL); bool quit = false; - SDL_Texture* texture_array[2] = {background, player}; - SDL_Rect* rect_array[2] = {&background_rect, &player_rect}; - int texturesState_array[2] = {1,1}; - + SDL_Texture* texture_array[3] = {background, sanya, kirill}; + SDL_Rect* rect_array[3] = {&background_rect, &sanya_rect, &kirill_rect}; + int texturesState_array[3] = {1,1,1}; while (!quit) { while(SDL_PollEvent(&event)) { SDL_PumpEvents(); // обработчик событий. - - if ((keyboardState[SDL_SCANCODE_UP])||(keyboardState[SDL_SCANCODE_W])) + + +std::cout << SDL_GetTicks() << std::endl; + + if (event.type == SDL_QUIT) { - move_UP(ren, player, player_rect); + quit = true; } - if ((keyboardState[SDL_SCANCODE_DOWN])||keyboardState[SDL_SCANCODE_S]) + if (event.type == SDL_MOUSEBUTTONDOWN) { - move_DOWN(ren, player, player_rect); + if (event.button.button == SDL_BUTTON_LEFT && event.button.x <= 10 && event.button.y <=10) + { + quit = true; + } + if (event.button.button == SDL_BUTTON_RIGHT) + { + texturesState_array[1] = 1; + } + if ((event.button.button == SDL_BUTTON_LEFT) && (event.button.x >= sanya_rect.x) && (event.button.y >= sanya_rect.y) && (event.button.x <= sanya_rect.w + sanya_rect.x) && (event.button.y <= sanya_rect.h + sanya_rect.y)) + { + texturesState_array[1] = 0; + } } - if ((keyboardState[SDL_SCANCODE_LEFT])||keyboardState[SDL_SCANCODE_A]) + if (keyboardState[SDL_SCANCODE_UP]) { - move_LEFT(ren, player, player_rect); + move_UP(ren, kirill, kirill_rect); } - if ((keyboardState[SDL_SCANCODE_RIGHT])||keyboardState[SDL_SCANCODE_D]) + if (keyboardState[SDL_SCANCODE_W]) { - move_RIGHT(ren, player, player_rect); + move_UP(ren, sanya, sanya_rect); + } + if (keyboardState[SDL_SCANCODE_DOWN]) + { + move_DOWN(ren, kirill, kirill_rect); + } + if (keyboardState[SDL_SCANCODE_S]) + { + move_DOWN(ren, sanya, sanya_rect); + } + if (keyboardState[SDL_SCANCODE_LEFT]) + { + move_LEFT(ren, kirill, kirill_rect); + } + if (keyboardState[SDL_SCANCODE_A]) + { + move_LEFT(ren, sanya, sanya_rect); + } + if (keyboardState[SDL_SCANCODE_RIGHT]) + { + move_RIGHT(ren, kirill, kirill_rect); + } + if (keyboardState[SDL_SCANCODE_D]) + { + move_RIGHT(ren, sanya, sanya_rect); } if (keyboardState[SDL_SCANCODE_KP_PLUS]) { texture_scale += 0.2; - player_rect.w = player_wight * texture_scale; - player_rect.h = player_height * texture_scale; + kirill_rect.w = player_wight * texture_scale; + kirill_rect.h = player_height * texture_scale; + sanya_rect.w = player_wight * texture_scale; + sanya_rect.h = player_height * texture_scale; } + ++sanya_rect.x; if (keyboardState[SDL_SCANCODE_KP_MINUS]) { texture_scale -= 0.2; - player_rect.w = player_wight * texture_scale; - player_rect.h = player_height * texture_scale; + kirill_rect.w = player_wight * texture_scale; + kirill_rect.h = player_height * texture_scale; + sanya_rect.w = player_wight * texture_scale; + sanya_rect.h = player_height * texture_scale; } if (keyboardState[SDL_SCANCODE_ESCAPE]) { quit = true; } render_UPDATE(ren, texture_array, rect_array, texturesState_array); - if (player_rect.x <= 0) - player_rect.x = 0; - if (player_rect.y <= 0) - player_rect.y = 0; SDL_RenderPresent(ren); } } - SDL_DestroyTexture(player); + SDL_DestroyTexture(sanya); + SDL_DestroyTexture(kirill); SDL_DestroyTexture(background); SDL_DestroyWindow(win); SDL_DestroyRenderer(ren); diff --git a/testSDL/lesson3/player1.png b/testSDL/lesson3/player1.png index 634fb5e..6791bd4 100644 Binary files a/testSDL/lesson3/player1.png and b/testSDL/lesson3/player1.png differ diff --git a/testSDL/lesson3/sanya.jpg b/testSDL/lesson3/sanya.jpg new file mode 100644 index 0000000..517d2ed Binary files /dev/null and b/testSDL/lesson3/sanya.jpg differ diff --git a/testSDL/lesson3/sanya_wings.png b/testSDL/lesson3/sanya_wings.png new file mode 100644 index 0000000..14d9e01 Binary files /dev/null and b/testSDL/lesson3/sanya_wings.png differ diff --git a/testSDL/lesson3/wings.png b/testSDL/lesson3/wings.png new file mode 100644 index 0000000..ba8f0e1 Binary files /dev/null and b/testSDL/lesson3/wings.png differ