Skip to content

Commit

Permalink
Alterações promovidas na aula do dia 13/04/16.
Browse files Browse the repository at this point in the history
  • Loading branch information
edsomjr committed Apr 13, 2016
1 parent 800b623 commit da2088c
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 93 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
*.swp
*.o
*~
lib
obj
*.d
16 changes: 16 additions & 0 deletions include/canvas.h
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
1 change: 0 additions & 1 deletion include/sdl2Dvideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace ijengine {
~SDL2DVideo();

Window * create_window(int w, int h);
SDL_Renderer * renderer;
};

}
Expand Down
21 changes: 21 additions & 0 deletions include/sdl2canvas.h
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
35 changes: 23 additions & 12 deletions include/sdl2texture.h
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
9 changes: 7 additions & 2 deletions include/sdl2window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
14 changes: 14 additions & 0 deletions include/texture.h
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
5 changes: 5 additions & 0 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
1 change: 1 addition & 0 deletions src/sdl2Dvideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/sdl2canvas.cpp
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);
}

}
8 changes: 8 additions & 0 deletions src/sdl2game.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "sdl2game.h"
#include "sdl2texture.h"
#include <SDL2/SDL_image.h>

#include <memory>
Expand All @@ -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);
}

}
69 changes: 26 additions & 43 deletions src/sdl2texture.cpp
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;
}
}
29 changes: 17 additions & 12 deletions src/sdl2window.cpp
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);
}
}
24 changes: 1 addition & 23 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit da2088c

Please sign in to comment.