From a40c9a899b9a4acb9b3ebc62d3e7c52b943877d5 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 7 Apr 2025 11:55:09 +0200 Subject: [PATCH] ja pinta el logo en tamany 1:1 moguda la logica del main a la classe Logo --- jailgames.gif | Bin 478 -> 98 bytes source/logo.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++ source/logo.h | 27 ++++++++++++++++ source/main.cpp | 73 +++--------------------------------------- source/screen.cpp | 1 + source/surface.cpp | 2 +- 6 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 source/logo.cpp create mode 100644 source/logo.h diff --git a/jailgames.gif b/jailgames.gif index adc09e8362fdc5e56e01ec98899e8bd2483218f7..5606819785cb134bf624042bed8491782e9e3299 100644 GIT binary patch literal 98 zcmZ?wbh9u|G-P0BXkY+=|Ns9h{$%0g0y1?#e2@$SlO>B#TV?06xD=jUu1{}ozxk?g wzIopw3CD*=&7>;IE_o$$N3JuEN>*H}bhN=%>3y;Gub54Kr=Bs1FfdpH0ElfNC;$Ke literal 478 zcmV<40U`cJNk%v~VYL7o0Du4h00030|NkNX3rz7s|(6l)+pT}^%c zI%AU@zRZ_y&ZYP_o~mtZR$?BHu0H+vZTn8Yj!z!6Yxck~Qa29SIZXH98H`r1;Xi%( z1S(AUEMT*TqU0ii<5p4J#dG@#@^NTw;tG>~7Sco6Pa(U52YpHGD1v1-muO(_LpD>T zK#;Y53Kgo6s3xD6Mg}Fx)Z$TyId9&4hVkN4ofM_=#0qsPPj~IQ4#Wm4>{yOu30ma{ z_N&v7W}_x8O2uy0vO{gsjrkUD(3XK?+Eq%37hjEsR}p5LwlUWwiuH=!SyM1$v2pWd UCR6z`init(); + logo_surface = std::make_shared("jailgames.gif"); + logo_sprite = std::make_unique(logo_surface); + logo_sprite->setPosition( + (options.game.width - logo_sprite->getWidth()) / 2, + (options.game.height- logo_sprite->getHeight()) / 2); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Logo start"); +} + +void Logo::close() +{ + Screen::get()->destroy(); + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nBye!"); + SDL_Quit(); +} + +void Logo::checkEvents() +{ + SDL_Event event; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_EVENT_QUIT: + running = false; + return; + } + + Mouse::handleEvent(event); + } +} + +void Logo::update() +{ + if (SDL_GetTicks() - ticks > options.game.speed) + { + ticks = SDL_GetTicks(); + } +} + +void Logo::render() +{ + Screen::get()->start(); + logo_sprite->render(); + Screen::get()->render(); +} + +int Logo::run() +{ + while (running) + { + update(); + checkEvents(); + render(); + } + return 0; +} \ No newline at end of file diff --git a/source/logo.h b/source/logo.h new file mode 100644 index 0000000..cb82d09 --- /dev/null +++ b/source/logo.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include "surface.h" +#include "s_sprite.h" + +class Logo +{ + +private: + bool running = true; + Uint64 ticks = 0; + std::shared_ptr logo_surface = nullptr; + std::unique_ptr logo_sprite = nullptr; + + void init(); + void close(); + void checkEvents(); + void update(); + void render(); + +public: + Logo(); + ~Logo(); + int run(); +}; \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 36c33c8..32a1c3d 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,75 +1,12 @@ #include #include -#include "screen.h" -#include "mouse.h" -#include "surface.h" -#include "s_sprite.h" - -bool running = true; -Uint64 ticks = 0; -std::shared_ptr logo_surface = nullptr; -std::unique_ptr logo_sprite = nullptr; - -void init() -{ - Screen::get()->init(); - logo_surface = std::make_shared("jailgames.gif"); - logo_sprite = std::make_unique(logo_surface); - - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Logo start"); -} - -void close() -{ - Screen::get()->destroy(); - - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "\nBye!"); - SDL_Quit(); -} - -void checkEvents() -{ - SDL_Event event; - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_EVENT_QUIT: - running = false; - return; - } - - Mouse::handleEvent(event); - } -} - -void update() -{ - if (SDL_GetTicks() - ticks > options.game.speed) - { - ticks = SDL_GetTicks(); - } -} - -void render() -{ - Screen::get()->start(); - - logo_sprite->render(); - - Screen::get()->render(); -} +#include "logo.h" int main() { - init(); + // Crea el objeto Logo + auto logo = std::make_unique(); - while (running) - { - update(); - checkEvents(); - render(); - } - - close(); + // Bucle principal + return logo->run(); } diff --git a/source/screen.cpp b/source/screen.cpp index cae9e48..9c8f0a7 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -50,6 +50,7 @@ Screen::Screen() std::cerr << "Error: game_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } } + SDL_SetTextureScaleMode(game_texture_, options.video.scale_mode); // Crea la surface donde se dibujan los graficos del juego game_surface_ = std::make_shared(options.game.width, options.game.height); diff --git a/source/surface.cpp b/source/surface.cpp index ce640e1..41cc184 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -494,7 +494,7 @@ void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture) int pitch = 0; // Bloquea la textura para modificar los pĂ­xeles directamente - if (SDL_LockTexture(texture, nullptr, reinterpret_cast(&pixels), &pitch) != 0) + if (!SDL_LockTexture(texture, nullptr, reinterpret_cast(&pixels), &pitch)) { throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError())); }