-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alterações promovidas na aula do dia 13/04/16.
- Loading branch information
Showing
14 changed files
with
174 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
*.swp | ||
*.o | ||
*~ | ||
lib | ||
obj | ||
*.d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef IJENGINE_SDL2CANVAS_H | ||
#define IJENGINE_SDL2CANVAS_H | ||
|
||
#include <SDL2/SDL.h> | ||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
#pragma once | ||
#include <iostream> | ||
#ifndef SDL2_TEXTURE_H | ||
#define SDL2_TEXTURE_H | ||
|
||
#include <SDL2/SDL.h> | ||
#include <string> | ||
|
||
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "sdl2canvas.h" | ||
#include "sdl2texture.h" | ||
|
||
#include <SDL2/SDL_image.h> | ||
|
||
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<const SDL2Texture *>(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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,52 @@ | ||
#include "sdl2texture.h" | ||
#include <iostream> | ||
#include "sdl2canvas.h" | ||
|
||
#include <SDL2/SDL.h> | ||
#include <SDL2/SDL_image.h> | ||
|
||
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<const SDL2Canvas *>(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,44 @@ | ||
#include "sdl2window.h" | ||
#include "sdl2canvas.h" | ||
#include <SDL2/SDL.h> | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters