From da2088c4e483e6a505eb544455334fcb3937db98 Mon Sep 17 00:00:00 2001 From: Edson Alves Date: Wed, 13 Apr 2016 16:42:51 -0300 Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=B5es=20promovidas=20na=20aula?= =?UTF-8?q?=20do=20dia=2013/04/16.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ include/canvas.h | 16 ++++++++++ include/sdl2Dvideo.h | 1 - include/sdl2canvas.h | 21 +++++++++++++ include/sdl2texture.h | 35 ++++++++++++++-------- include/sdl2window.h | 9 ++++-- include/texture.h | 14 +++++++++ include/window.h | 5 ++++ src/sdl2Dvideo.cpp | 1 + src/sdl2canvas.cpp | 32 ++++++++++++++++++++ src/sdl2game.cpp | 8 +++++ src/sdl2texture.cpp | 69 ++++++++++++++++--------------------------- src/sdl2window.cpp | 29 ++++++++++-------- test/main.cpp | 24 +-------------- 14 files changed, 174 insertions(+), 93 deletions(-) create mode 100644 include/canvas.h create mode 100644 include/sdl2canvas.h create mode 100644 include/texture.h create mode 100644 src/sdl2canvas.cpp diff --git a/.gitignore b/.gitignore index 2558dae..48ffd15 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *.swp *.o *~ +lib +obj +*.d diff --git a/include/canvas.h b/include/canvas.h new file mode 100644 index 0000000..3d27481 --- /dev/null +++ b/include/canvas.h @@ -0,0 +1,16 @@ +#ifndef IJENGINE_CANVAS_H +#define IJENGINE_CANVAS_H + +namespace ijengine { + + class Texture; + + class Canvas { + public: + virtual ~Canvas() = default; + virtual void draw(const Texture *texture, int x, int y) = 0; + virtual void update() = 0; + }; +} + +#endif diff --git a/include/sdl2Dvideo.h b/include/sdl2Dvideo.h index 7b9c7ae..e7c66e6 100644 --- a/include/sdl2Dvideo.h +++ b/include/sdl2Dvideo.h @@ -12,7 +12,6 @@ namespace ijengine { ~SDL2DVideo(); Window * create_window(int w, int h); - SDL_Renderer * renderer; }; } diff --git a/include/sdl2canvas.h b/include/sdl2canvas.h new file mode 100644 index 0000000..7c288ca --- /dev/null +++ b/include/sdl2canvas.h @@ -0,0 +1,21 @@ +#ifndef IJENGINE_SDL2CANVAS_H +#define IJENGINE_SDL2CANVAS_H + +#include +#include "canvas.h" + +namespace ijengine { + + class SDL2Canvas : public Canvas { + public: + SDL2Canvas(SDL_Renderer *renderer); + void draw(const Texture *texture, int x, int y); + + SDL_Renderer * renderer() const { return m_renderer; } + void update(); + private: + SDL_Renderer *m_renderer; + }; +} + +#endif diff --git a/include/sdl2texture.h b/include/sdl2texture.h index 7e280d0..48ff36f 100644 --- a/include/sdl2texture.h +++ b/include/sdl2texture.h @@ -1,22 +1,33 @@ -#pragma once -#include +#ifndef SDL2_TEXTURE_H +#define SDL2_TEXTURE_H + #include +#include + +#include "texture.h" +#include "canvas.h" + +using std::string; namespace ijengine { - class SDL2Texture { + class SDL2Texture : public Texture { public: - SDL2Texture(std::string file_path, SDL_Renderer *actual_renderer); - int w; - int h; - bool valid_texture(); - void load_texture_from_path(); + SDL2Texture(SDL_Texture *texture, int w, int h); ~SDL2Texture(); - void update(); + + static SDL2Texture * load_texture_from_path(const string& path, const Canvas *c); + + SDL_Texture * texture() const; + + int w() const { return m_w; } + int h() const { return m_h; } private: - std::string file_name; - SDL_Renderer *renderer; - SDL_Texture *sdl_texture; + SDL_Texture *m_texture; + int m_w; + int m_h; }; } + +#endif diff --git a/include/sdl2window.h b/include/sdl2window.h index e636fa2..0f7767d 100644 --- a/include/sdl2window.h +++ b/include/sdl2window.h @@ -10,12 +10,17 @@ namespace ijengine { public: SDL2Window(SDL_Window *window, SDL_Renderer *renderer); ~SDL2Window(); + int w() const; int h() const; + Canvas * canvas() const; private: - SDL_Window *sdl_window; - SDL_Renderer *renderer; + SDL_Window *m_window; + SDL_Renderer *m_renderer; + + int m_w; + int m_h; }; } diff --git a/include/texture.h b/include/texture.h new file mode 100644 index 0000000..91bfe24 --- /dev/null +++ b/include/texture.h @@ -0,0 +1,14 @@ +#ifndef TEXTURE_H +#define TEXTURE_H + +namespace ijengine { + + class Texture { + public: + virtual ~Texture() = default; + virtual int w() const = 0; + virtual int h() const = 0; + }; +} + +#endif diff --git a/include/window.h b/include/window.h index 5dcc487..cf02dab 100644 --- a/include/window.h +++ b/include/window.h @@ -3,11 +3,16 @@ namespace ijengine { + class Canvas; + class Window { public: virtual ~Window() = default; + virtual int w() const = 0; virtual int h() const = 0; + + virtual Canvas * canvas() const = 0; }; } diff --git a/src/sdl2Dvideo.cpp b/src/sdl2Dvideo.cpp index 36c24a0..da9c183 100644 --- a/src/sdl2Dvideo.cpp +++ b/src/sdl2Dvideo.cpp @@ -21,6 +21,7 @@ namespace ijengine { SDL2DVideo::create_window(int w, int h) { SDL_Window *window; + SDL_Renderer *renderer; if (SDL_CreateWindowAndRenderer(w, h, 0, &window, &renderer)) return nullptr; diff --git a/src/sdl2canvas.cpp b/src/sdl2canvas.cpp new file mode 100644 index 0000000..83fb19f --- /dev/null +++ b/src/sdl2canvas.cpp @@ -0,0 +1,32 @@ +#include "sdl2canvas.h" +#include "sdl2texture.h" + +#include + +namespace ijengine { + +SDL2Canvas::SDL2Canvas(SDL_Renderer *renderer) + : m_renderer(renderer) +{ +} + +void +SDL2Canvas::draw(const Texture *texture, int x, int y) +{ + const SDL2Texture *text = dynamic_cast(texture); + + SDL_SetRenderDrawColor(m_renderer, 255, 255, 255, 0); + SDL_RenderClear(m_renderer); + + SDL_Rect rect { x, y, text->w(), text->h() }; + SDL_RenderCopy(m_renderer, text->texture(), nullptr, nullptr); + SDL_RenderPresent(m_renderer); +} + +void +SDL2Canvas::update() +{ + SDL_RenderPresent(m_renderer); +} + +} diff --git a/src/sdl2game.cpp b/src/sdl2game.cpp index d56ad6f..8fe5ead 100644 --- a/src/sdl2game.cpp +++ b/src/sdl2game.cpp @@ -1,4 +1,5 @@ #include "sdl2game.h" +#include "sdl2texture.h" #include #include @@ -22,6 +23,13 @@ namespace ijengine { if (not m_window) throw "Error on SDL2Game::SDL2Game()"; + + SDL2Texture *tex = SDL2Texture::load_texture_from_path("test/img-test.png", m_window->canvas()); + + m_window->canvas()->draw(tex, 0, 0); + m_window->canvas()->update(); + + SDL_Delay(3000); } } diff --git a/src/sdl2texture.cpp b/src/sdl2texture.cpp index 2278774..f514ac5 100644 --- a/src/sdl2texture.cpp +++ b/src/sdl2texture.cpp @@ -1,69 +1,52 @@ #include "sdl2texture.h" -#include +#include "sdl2canvas.h" + #include #include namespace ijengine { - SDL2Texture::SDL2Texture(std::string file_path, SDL_Renderer *actual_renderer) : - file_name(file_path), - renderer(actual_renderer) + SDL2Texture::SDL2Texture(SDL_Texture *t, int w, int h) + : m_texture(t), m_w(w), m_h(h) { - if (valid_texture()) { - load_texture_from_path(); - } } - bool - SDL2Texture::valid_texture() + SDL2Texture * + SDL2Texture::load_texture_from_path(const string& path, const Canvas *c) { - bool valid = true; - if (file_name.empty()) { - valid = false; - } + SDL_Surface *surface_from_img = IMG_Load(path.c_str()); - if (renderer == nullptr) { - valid = false; - } + if (not surface_from_img) + return nullptr; - return valid; - } + const SDL2Canvas *canvas = dynamic_cast(c); + SDL_Renderer *r = canvas->renderer(); - void - SDL2Texture::load_texture_from_path() - { - SDL_Surface *surface_from_img = IMG_Load(file_name.c_str()); - SDL_Texture *texture_from_surface = nullptr; + SDL_Texture *texture_from_surface = SDL_CreateTextureFromSurface(r, surface_from_img); + if (not texture_from_surface) + { + SDL_FreeSurface(surface_from_img); + return nullptr; + } - if (surface_from_img != nullptr) { - texture_from_surface = SDL_CreateTextureFromSurface(renderer, surface_from_img); - if (texture_from_surface != nullptr) { - w = surface_from_img->w; - h = surface_from_img->h; + int w = surface_from_img->w; + int h = surface_from_img->h; - sdl_texture = texture_from_surface; - } else { - std::cout << SDL_GetError() << std::endl; - } + SDL_FreeSurface(surface_from_img); - SDL_FreeSurface(surface_from_img); - } else { - std::cout << SDL_GetError() << std::endl; - } + return new SDL2Texture(texture_from_surface, w, h); } SDL2Texture::~SDL2Texture() { - if (sdl_texture != nullptr) { - SDL_DestroyTexture(sdl_texture); + if (m_texture) { + SDL_DestroyTexture(m_texture); } } - void - SDL2Texture::update() + SDL_Texture * + SDL2Texture::texture() const { - if (renderer != nullptr) { - SDL_RenderCopy(renderer, sdl_texture, nullptr, nullptr); - } + return m_texture; } } diff --git a/src/sdl2window.cpp b/src/sdl2window.cpp index 09f4d33..be2572c 100644 --- a/src/sdl2window.cpp +++ b/src/sdl2window.cpp @@ -1,39 +1,44 @@ #include "sdl2window.h" +#include "sdl2canvas.h" #include namespace ijengine { SDL2Window::SDL2Window(SDL_Window *new_window, SDL_Renderer *actual_renderer) : - sdl_window(new_window), - renderer(actual_renderer) + m_window(new_window), m_renderer(actual_renderer), m_w(0), m_h(0) { + if (m_window) + { + SDL_GetWindowSize(m_window, &m_w, &m_h); + } } SDL2Window::~SDL2Window() { - /* - WARNING: destroying this window will also destroy its renderer.. - */ - - if (renderer != nullptr) { - SDL_DestroyRenderer(renderer); + if (m_renderer) { + SDL_DestroyRenderer(m_renderer); } - if (sdl_window != nullptr) { - SDL_DestroyWindow(sdl_window); + if (m_window) { + SDL_DestroyWindow(m_window); } } int SDL2Window::w() const { - return 0; + return m_w; } int SDL2Window::h() const { - return 0; + return m_h; } + Canvas * + SDL2Window::canvas() const + { + return new SDL2Canvas(m_renderer); + } } diff --git a/test/main.cpp b/test/main.cpp index 5f8473e..a6e22a3 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -8,27 +8,5 @@ int main() { SDL2Game game; - int fail = game.run(); - - if (not fail) { - SDL2Texture test_texture("test/img-test.png", game.m_video->renderer); - - SDL_Event e; - bool quit = false; - - while (not quit) { - while (SDL_PollEvent(&e) != 0) { - if (e.type == SDL_QUIT) { - quit = true; - } - } - SDL_RenderClear(game.m_video->renderer); - SDL_SetRenderDrawColor(game.m_video->renderer, 0xFF, 0xFF, 0xFF, 0xFF); - - test_texture.update(); - SDL_RenderPresent(game.m_video->renderer); - } - } - - return fail; + return 0; }