├── .gitignore ├── README.md ├── docs ├── Plano_de_Ensino.pdf └── Termo_de_Ciencia.pdf ├── engine ├── Makefile ├── Makefile.win ├── cpplint.py ├── include │ ├── core │ │ ├── animation.h │ │ ├── audiomanager.h │ │ ├── audiomanagerwrapper.h │ │ ├── bitmap.h │ │ ├── camera.h │ │ ├── canvas.h │ │ ├── circle.h │ │ ├── color.h │ │ ├── environment.h │ │ ├── eventsmanager.h │ │ ├── exception.h │ │ ├── font.h │ │ ├── game.h │ │ ├── image.h │ │ ├── joystickevent.h │ │ ├── keyboardevent.h │ │ ├── level.h │ │ ├── line.h │ │ ├── listener.h │ │ ├── mousebuttonevent.h │ │ ├── mousemotionevent.h │ │ ├── music.h │ │ ├── object.h │ │ ├── point.h │ │ ├── rect.h │ │ ├── resourcesmanager.h │ │ ├── settings.h │ │ ├── soundeffect.h │ │ ├── sprite.h │ │ ├── systemevent.h │ │ ├── text.h │ │ ├── texture.h │ │ └── video.h │ └── util │ │ ├── button.h │ │ └── frontend.h ├── lint.sh ├── res │ ├── dll │ │ └── windows.zip │ ├── fonts │ │ └── FLATS.ttf │ └── images │ │ ├── hexagon.png │ │ ├── idle.png │ │ ├── image_credits.txt │ │ ├── running.png │ │ ├── spiral.png │ │ └── star.png ├── src │ ├── animation.cpp │ ├── audiomanager.cpp │ ├── audiomanagerwrapper.cpp │ ├── bitmap.cpp │ ├── button.cpp │ ├── camera.cpp │ ├── canvas.cpp │ ├── circle.cpp │ ├── color.cpp │ ├── environment.cpp │ ├── eventsmanager.cpp │ ├── font.cpp │ ├── frontend.cpp │ ├── game.cpp │ ├── image.cpp │ ├── joystickevent.cpp │ ├── keyboardevent.cpp │ ├── level.cpp │ ├── line.cpp │ ├── listener.cpp │ ├── mousebuttonevent.cpp │ ├── mousemotionevent.cpp │ ├── music.cpp │ ├── object.cpp │ ├── point.cpp │ ├── rect.cpp │ ├── resourcesmanager.cpp │ ├── settings.cpp │ ├── soundeffect.cpp │ ├── sprite.cpp │ ├── systemevent.cpp │ ├── text.cpp │ ├── texture.cpp │ └── video.cpp └── test │ ├── dinoman.cpp │ ├── dinoman.h │ ├── main.cpp │ ├── stage.cpp │ ├── stage.h │ ├── test.cpp │ ├── test.h │ ├── titlescreen.cpp │ └── titlescreen.h ├── maps ├── arquitetura_de_um_jogo.png └── ciclo_de_vida.png └── tasks ├── D001_Versao_Inicial_do_Documento_de_Visao.md ├── D002_Versao_Final_do_Documento_de_Visao.md ├── D003_Versao_Inicial_do_GDD.md ├── D004_GDD_Requisitos_Tecnologicos.md ├── D005_GDD_Front_End.md ├── D006_GDD_Telas.md ├── D007_Camera_e_HUD.md ├── D008_Personagem_Principal.md ├── D009_Power_Ups.md ├── D010_Saude.md ├── D011_Sistema_de_Pontuacao.md ├── D012_Economia.md ├── D013_Principais_Personagens_do_Mundo_do_Jogo.md ├── D014_Veiculos.md ├── D015_Progresso_do_Jogo.md ├── D016_Visao_Geral_do_Mundo_do_Jogo.md ├── D017_Mecanicas_Universais.md ├── D018_Niveis.md ├── D019_Inimigos.md ├── D020_NPCs.md ├── D021_Objetos_Colecionaveis.md ├── D022_Minigames.md ├── D023_Musicas_e_Efeitos_Sonoros.md ├── G001_Brainstorm.md ├── G002_Conceito_do_Jogo.md ├── G003_Documento_de_Apresentacao_da_Equipe.md ├── G004_Cronograma.md ├── G005_Plano_de_Negocio.md ├── G006_Low_Fi_Sketches.md ├── G007_Hi_Fi_Sketches.md ├── G008_Musicas.md ├── G009_Efeitos_Sonoros.md ├── G010_Arte_Definitiva.md ├── G011_Instalador.md ├── G012_Manual.md ├── Milestones.md ├── P001_Ambiente_de_Desenvolvimento.md ├── P002_Laco_Principal.md ├── P003_Janela.md ├── P004_Modulo_de_Video.md ├── P005_Imagens.md ├── P006_Tela_de_Apresentacao.md ├── P007_Controles.md ├── P008_Animacoes.md ├── P009_Gameplay.md ├── P010_Textos.md ├── P011_Audio.md ├── P012_Rede.md ├── P013_Primeira_Fase_Jogavel.md ├── P014_Versao_Alfa.md ├── P015_Versao_Beta.md └── T000_Formacao_da_Equipe.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.bak 2 | *~ 3 | *.o 4 | *.swp 5 | *.d 6 | *.komodoproject 7 | *.exe 8 | *.dll 9 | lib 10 | engine/test/res 11 | engine/test/test 12 | *.tar.gz 13 | *.log 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # IJE 2 | 3 | Material da disciplina Introdução aos Jogos Eletrônicos da Faculdade UnB Gama 4 | 5 | ## Jogos Desenvolvidos 6 | 7 | Nesta seção estão todos os jogos desenvolvidos nesta disciplina, separados por 8 | semestre. No total, foram desenvolvidos 20 jogos até a última atualização. 9 | 10 | 11 | **1/2016** 12 | * [Deadly Wish](https://github.com/fgagamedev/Deadly-Wish) 13 | * [Strife of Mythology](https://github.com/fgagamedev/Strife-of-Mythology) 14 | * [Travelling Will](https://github.com/fgagamedev/Traveling-Will) 15 | 16 | **1/2015** 17 | * [7 Keys](https://github.com/fgagamedev/7-Keys) 18 | * [Babel](https://github.com/fgagamedev/Babel) 19 | * [Terracota](https://github.com/fgagamedev/Terracota) 20 | 21 | **1/2014** 22 | 23 | * [Dauphine](https://github.com/fgagamedev/Dauphine) 24 | * [Imagina na Copa](https://github.com/fgagamedev/Imagina-na-Copa) 25 | * [Kays Against the World](https://github.com/fgagamedev/Kays-Against-the-World) 26 | 27 | **2/2013** 28 | 29 | * [Ankhnowledge](https://github.com/fgagamedev/Ankhnowledge) 30 | * [The Last World War](https://github.com/fgagamedev/LastWorldWar) 31 | * [Post War](https://github.com/fgagamedev/Post-War) 32 | * [War of the nets](https://github.com/fgagamedev/War-of-the-Nets) 33 | 34 | **1/2013** 35 | 36 | * [Jack the Janitor](https://github.com/fgagamedev/Jack-The-Janitor) 37 | 38 | **2/2012** 39 | 40 | * **Drawing Attack** 41 | * **Earth Attacks** 42 | * [Emperor vs Aliens](https://github.com/fgagamedev/Emperor-vs-Aliens) 43 | * [Ninja Siege](https://github.com/fgagamedev/Ninja-Siege) 44 | * [Space monkeys](https://github.com/fgagamedev/Space-Monkey) 45 | * **Tacape** 46 | 47 | ## Professores 48 | - Edson Alves da Costa Junior (@edsomjr) 49 | - Matheus de Sousa Faria (@matheusfaria) 50 | 51 | ## Monitores 52 | - Simião Correia Lima de Carvalho (@simiaosimis) 53 | -------------------------------------------------------------------------------- /docs/Plano_de_Ensino.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/docs/Plano_de_Ensino.pdf -------------------------------------------------------------------------------- /docs/Termo_de_Ciencia.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/docs/Termo_de_Ciencia.pdf -------------------------------------------------------------------------------- /engine/Makefile: -------------------------------------------------------------------------------- 1 | NAME = ijengine 2 | VERSION = 0.0.5 3 | 4 | SRC_DIR = src 5 | INC_DIR = include 6 | OBJ_DIR = obj 7 | LIB_DIR = lib 8 | TST_DIR = test 9 | 10 | INSTALL_DIR = /usr/local 11 | INCLUDES_INSTALL_DIR = $(INSTALL_DIR)/include/$(NAME) 12 | LIB_INSTALL_DIR = $(INSTALL_DIR)/lib 13 | 14 | PACKAGE = $(NAME)-$(VERSION).tar.gz 15 | 16 | CC = g++ 17 | AR = ar 18 | 19 | ARFLAGS = rcs 20 | CFLAGS = -pedantic -std=c++11 -MMD -g3 -g\ 21 | -W -Wall -Wextra -Wshadow -Wcast-align -Wcast-qual -Wctor-dtor-privacy\ 22 | -Wdisabled-optimization -Wformat=2 -Wlogical-op -Wmissing-declarations\ 23 | -Wmissing-include-dirs -Wnoexcept -Woverloaded-virtual -Wredundant-decls\ 24 | -Wsign-promo -Wstrict-null-sentinel -Wswitch-default -Wundef\ 25 | -Wzero-as-null-pointer-constant -Wuseless-cast -Wnon-virtual-dtor 26 | INCLUDES = -Iinclude -Itest `sdl2-config --cflags` 27 | LIBS = `sdl2-config --libs` -lSDL2_image -lSDL2_ttf -lSDL2_mixer 28 | 29 | TARGET = $(LIB_DIR)/lib$(NAME).a 30 | 31 | TEST = $(TST_DIR)/test 32 | TST_SRC = ${wildcard $(TST_DIR)/*.cpp} 33 | TST_OBJ = ${addprefix $(TST_DIR)/, ${notdir ${TST_SRC:.cpp=.o}}} 34 | 35 | SRC = ${wildcard $(SRC_DIR)/*.cpp} 36 | OBJ = ${addprefix $(OBJ_DIR)/, ${notdir ${SRC:.cpp=.o}}} 37 | 38 | .PHONY: clean depend dist-clean dist install uninstall 39 | 40 | all: 41 | @mkdir -p $(OBJ_DIR) $(LIB_DIR) 42 | $(MAKE) $(TARGET) 43 | 44 | $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 45 | @echo Building $@ 46 | @$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ 47 | 48 | $(TST_DIR)/%.o: $(TST_DIR)/%.cpp 49 | @echo Building $@ 50 | @$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ 51 | 52 | $(TARGET): $(OBJ) 53 | @$(AR) $(ARFLAGS) $@ $(OBJ) 54 | 55 | $(TEST): $(TST_OBJ) $(TARGET) 56 | @echo Building executable... 57 | @$(CC) $(CFLAGS) $(INCLUDES) $(TST_OBJ) $(TARGET) -o $@ $(LIBS) 58 | 59 | clean: 60 | @echo Cleaning... 61 | @find . -name *.o -exec rm {} \; 62 | @find . -name *.d -exec rm {} \; 63 | @rm -rf *~ *.o prog out.txt 64 | 65 | dist: 66 | @rm -rf *~ 67 | @tar czf $(PACKAGE) include src Makefile 68 | 69 | dist-clean: clean 70 | @find . -name *.a -exec rm {} \; 71 | @rm -f test/test $(TARGET) 72 | @rm -rf *.tar.gz 73 | 74 | install: $(TARGET) 75 | @mkdir -p $(INCLUDES_INSTALL_DIR) $(LIB_INSTALL_DIR) 76 | @cp -rf include/* $(INCLUDES_INSTALL_DIR) 77 | @cp -f $(TARGET) $(LIB_INSTALL_DIR) 78 | 79 | uninstall: 80 | @rm -rf $(INCLUDES_INSTALL_DIR) $(INSTALL_DIR)/$(TARGET) 81 | 82 | -include $(OBJ:.o=.d) 83 | -------------------------------------------------------------------------------- /engine/Makefile.win: -------------------------------------------------------------------------------- 1 | NAME = ijengine 2 | VERSION = 0.0.3 3 | 4 | SRC_DIR = src 5 | INC_DIR = include 6 | OBJ_DIR = obj 7 | LIB_DIR = lib 8 | TST_DIR = test 9 | RES_DIR = res 10 | DLL_DIR = res/dll 11 | 12 | INSTALL_DIR = /usr/x86_64-w64-mingw32 13 | INCLUDES_INSTALL_DIR = $(INSTALL_DIR)/include/$(NAME) 14 | LIB_INSTALL_DIR = $(INSTALL_DIR)/lib 15 | 16 | PACKAGE = $(NAME)-$(VERSION).tar.gz 17 | 18 | CC = x86_64-w64-mingw32-g++ 19 | AR = ar 20 | MINGW_ROOT = $(INSTALL_DIR) 21 | 22 | ARFLAGS = rcs 23 | CFLAGS = -W -Wall -pedantic -ansi -std=c++11 -MMD 24 | INCLUDES = -Iinclude -Itest `$(MINGW_ROOT)/bin/sdl2-config --cflags` 25 | LIBS = `$(MINGW_ROOT)/bin/sdl2-config --libs` -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lmingw32 26 | 27 | TARGET = $(LIB_DIR)/lib$(NAME).a 28 | 29 | TEST = $(TST_DIR)/test 30 | TST_SRC = ${wildcard $(TST_DIR)/*.cpp} 31 | TST_OBJ = ${addprefix $(TST_DIR)/, ${notdir ${TST_SRC:.cpp=.o}}} 32 | 33 | SRC = ${wildcard $(SRC_DIR)/*.cpp} 34 | OBJ = ${addprefix $(OBJ_DIR)/, ${notdir ${SRC:.cpp=.o}}} 35 | 36 | .PHONY: clean depend 37 | 38 | all: 39 | @mkdir -p $(OBJ_DIR) $(LIB_DIR) 40 | @make -f Makefile.win $(TARGET) 41 | 42 | $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 43 | @echo Building $^ 44 | @$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ 45 | 46 | $(TST_DIR)/%.o: $(TST_DIR)/%.cpp 47 | @echo Building $^ 48 | @$(CC) -c $(CFLAGS) $(INCLUDES) $< -o $@ 49 | 50 | $(TARGET): $(OBJ) 51 | @$(AR) $(ARFLAGS) $@ $(OBJ) 52 | 53 | $(TEST): $(TST_OBJ) $(TARGET) 54 | @cp -r $(RES_DIR) $(TST_DIR) 55 | @unzip -j $(DLL_DIR)/windows.zip -d $(TST_DIR) 56 | @echo "Building executable..." 57 | @$(CC) $(CFLAGS) $(INCLUDES) $(TST_OBJ) $(TARGET) -o $@.exe $(LIBS) 58 | 59 | clean: 60 | @echo Cleaning... 61 | @find . -name *.o -exec rm {} \; 62 | @find . -name *.d -exec rm {} \; 63 | @find . -name *.dll -exec rm {} \; 64 | @rm -rf *~ *.o prog out.txt test/res 65 | 66 | dist: 67 | @rm -rf *~ 68 | @tar czf $(PACKAGE) include src Makefile 69 | 70 | dist-clean: clean 71 | @find . -name *.a -exec rm {} \; 72 | @rm -f test/test.exe $(TARGET) 73 | @rm -rf *.tar.gz 74 | 75 | install: $(TARGET) 76 | @mkdir -p $(INCLUDES_INSTALL_DIR) $(LIB_INSTALL_DIR) 77 | @cp -rf include/* $(INCLUDES_INSTALL_DIR) 78 | @cp -f $(TARGET) $(LIB_INSTALL_DIR) 79 | 80 | uninstall: 81 | @rm -rf $(INCLUDES_INSTALL_DIR) $(INSTALL_DIR)/$(TARGET) 82 | 83 | -include $(OBJ:.o=.d) 84 | -------------------------------------------------------------------------------- /engine/include/core/animation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma animação. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 05/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef ANIMATION_H 9 | #define ANIMATION_H 10 | 11 | #include 12 | #include 13 | 14 | using std::string; 15 | using std::unique_ptr; 16 | 17 | 18 | class Animation 19 | { 20 | public: 21 | Animation(const string& image, double x, double y, double w, double h, 22 | int frames, unsigned long speed_in_ms, bool loop = false); 23 | ~Animation(); 24 | 25 | bool is_done() const; 26 | 27 | void update(unsigned long elapsed); 28 | void draw(double x, double y); 29 | 30 | double w() const; 31 | double h() const; 32 | 33 | void set_row(int row); 34 | 35 | void reset(); 36 | 37 | private: 38 | class Impl; 39 | unique_ptr m_impl; 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /engine/include/core/audiomanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa o sistema de audio. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef AUDIOMANAGER_H 9 | #define AUDIOMANAGER_H 10 | 11 | #include 12 | 13 | using std::string; 14 | 15 | static constexpr int k_infinite_loop = -1; 16 | 17 | enum class AudioType 18 | { 19 | Music, 20 | SoundEffect 21 | }; 22 | 23 | template 24 | class AudioManager final 25 | { 26 | public: 27 | AudioManager(); 28 | 29 | void play(const string& path, const int times); 30 | void set_volume(const int percent); 31 | void pause(); 32 | void resume(); 33 | void stop(); 34 | void fade_out(const double seconds); 35 | bool faded_out(); 36 | }; 37 | 38 | using AudioManagerSfx = AudioManager; 39 | using AudioManagerMusic = AudioManager; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /engine/include/core/audiomanagerwrapper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa a interface do sistema de audio. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 18/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef AUDIOMANAGERWRAPPER_H 9 | #define AUDIOMANAGERWRAPPER_H 10 | 11 | #include "core/audiomanager.h" 12 | #include "core/exception.h" 13 | 14 | class AudioManagerWrapper 15 | { 16 | public: 17 | AudioManagerWrapper(); 18 | ~AudioManagerWrapper(); 19 | 20 | void init() throw (Exception); 21 | 22 | AudioManagerSfx * sfx() const; 23 | AudioManagerMusic * music() const; 24 | 25 | private: 26 | void open_audio(); 27 | void close_audio(); 28 | 29 | AudioManagerSfx *sfx_manager; 30 | AudioManagerMusic *music_manager; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /engine/include/core/bitmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um mapa de bits. 3 | * 4 | * Autor: Rodrigo Gonçalves 5 | * Data: 18/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef BITMAP_H 9 | #define BITMAP_H 10 | 11 | #include "core/exception.h" 12 | #include "core/canvas.h" 13 | 14 | #include 15 | #include 16 | 17 | using std::string; 18 | using std::unique_ptr; 19 | 20 | class Bitmap 21 | { 22 | public: 23 | Bitmap(void *data, int w, int h); 24 | Bitmap(Canvas *canvas); 25 | ~Bitmap(); 26 | 27 | int w() const; 28 | int h() const; 29 | void * data() const; 30 | void * pixels() const; 31 | void clear(); 32 | 33 | Uint32 getpixel(SDL_Surface *surface, int x, int y); 34 | void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel); 35 | 36 | static Bitmap * from_file(const string& path) throw (Exception); 37 | 38 | void fill(const Rect& r, Uint32 color); 39 | 40 | private: 41 | class Impl; 42 | unique_ptr m_impl; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /engine/include/core/camera.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma camera. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 27/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef CAMERA_H 9 | #define CAMERA_H 10 | 11 | #include "core/object.h" 12 | 13 | #include 14 | 15 | using std::unique_ptr; 16 | 17 | class Camera : public Object 18 | { 19 | public: 20 | typedef enum { STATIC, FOLLOWING } Mode; 21 | 22 | Camera(double x = 0, double y = 0, double w = 1, double h = 1, 23 | Mode mode = STATIC); 24 | ~Camera(); 25 | 26 | Mode mode() const; 27 | void set_mode(Mode mode); 28 | 29 | void follow(const Object* object); 30 | 31 | void set_limits(const Rect& limits); 32 | 33 | private: 34 | class Impl; 35 | unique_ptr m_impl; 36 | 37 | void update_self(unsigned long elapsed); 38 | void draw_self(); 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /engine/include/core/canvas.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa a área da janela a ser desenhada. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef CANVAS_H 9 | #define CANVAS_H 10 | 11 | #include 12 | #include 13 | 14 | #include "exception.h" 15 | #include "color.h" 16 | 17 | using std::string; 18 | using std::shared_ptr; 19 | 20 | class Point; 21 | class Line; 22 | class Rect; 23 | class Circle; 24 | class Texture; 25 | class Font; 26 | class Bitmap; 27 | 28 | class Canvas 29 | { 30 | public: 31 | typedef enum { NONE, BLEND } BlendMode; 32 | 33 | Canvas(SDL_Renderer *renderer, int w, int h); 34 | ~Canvas(); 35 | 36 | int w() const; 37 | int h() const; 38 | const Color& color() const; 39 | shared_ptr font() const; 40 | BlendMode blend_mode() const; 41 | 42 | void set_resolution(int w, int h); 43 | void set_color(const Color& color); 44 | void set_font(shared_ptr& font); 45 | void set_blend_mode(BlendMode mode); 46 | 47 | void clear(const Color& color = Color::BLACK); 48 | void update(); 49 | 50 | void draw(const Point& point) const; 51 | void draw(const Point& point, const Color& color); 52 | 53 | void draw(const Line& line) const; 54 | void draw(const Line& line, const Color& color); 55 | 56 | void draw(const Rect& rect) const; 57 | void draw(const Rect& rect, const Color& color); 58 | 59 | void draw(const Circle& circle) const; 60 | void draw(const Circle& circle, const Color& color); 61 | 62 | void draw(const Texture *texture, double x = 0, double y = 0, double w = 0, double h = 0) const; 63 | void draw(const Texture *texture, Rect clip, double x = 0, 64 | double y = 0, double w = 0, double h = 0) const; 65 | 66 | void draw(const string& text, double x = 0, double y = 0, 67 | const Color& color = Color::WHITE) const; 68 | 69 | void draw(const Bitmap *bitmap, double x = 0, double y = 0) const; 70 | 71 | void fill(const Rect& rect) const; 72 | void fill(const Rect& rect, const Color& color); 73 | 74 | void fill(const Circle& circle) const; 75 | void fill(const Circle& circle, const Color& color); 76 | 77 | Texture * render_text(const string& text, const Color& color); 78 | 79 | SDL_Renderer * renderer() const; 80 | SDL_Surface * bitmap() const; 81 | void update_bitmap(); 82 | 83 | private: 84 | SDL_Renderer *m_renderer; 85 | int m_w, m_h; 86 | Color m_color; 87 | shared_ptr m_font; 88 | BlendMode m_blend_mode; 89 | SDL_Surface *m_bitmap; 90 | SDL_Texture *m_texture; 91 | 92 | void draw_circle_points(int cx, int cy, int x, int y) const; 93 | void fill_circle_points(int cx, int cy, int x, int y) const; 94 | }; 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /engine/include/core/circle.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um círculo no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef CIRCLE_H 9 | #define CIRCLE_H 10 | 11 | #include "point.h" 12 | 13 | class Circle 14 | { 15 | public: 16 | Circle(const Point& center = Point(), double radius = 0); 17 | 18 | Point center() const; 19 | double radius() const; 20 | 21 | void set_center(const Point& center); 22 | void set_radius(double radius); 23 | void set(const Point& center, double radius); 24 | 25 | private: 26 | Point m_center; 27 | double m_r; 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /engine/include/core/color.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma cor. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef COLOR_H 9 | #define COLOR_H 10 | 11 | class Color 12 | { 13 | public: 14 | Color(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 255); 15 | 16 | bool operator!=(const Color& color) const; 17 | 18 | unsigned char r() const; 19 | unsigned char g() const; 20 | unsigned char b() const; 21 | unsigned char a() const; 22 | 23 | void set_r(unsigned char r); 24 | void set_g(unsigned char g); 25 | void set_b(unsigned char b); 26 | void set_a(unsigned char a); 27 | void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255); 28 | 29 | static Color WHITE; 30 | static Color BLACK; 31 | static Color RED; 32 | static Color GREEN; 33 | static Color BLUE; 34 | static Color YELLOW; 35 | static Color CYAN; 36 | static Color MAGENTA; 37 | static Color TRANSPARENT; 38 | 39 | private: 40 | unsigned char m_r, m_g, m_b, m_a; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /engine/include/core/environment.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Singleton que contém as variáveis globais da engine. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 06/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef ENVIRONMENT_H 9 | #define ENVIRONMENT_H 10 | 11 | #include "exception.h" 12 | #include "video.h" 13 | #include "canvas.h" 14 | #include "resourcesmanager.h" 15 | #include "eventsmanager.h" 16 | #include "audiomanagerwrapper.h" 17 | #include "camera.h" 18 | 19 | class Environment 20 | { 21 | public: 22 | ~Environment(); 23 | 24 | static Environment * get_instance() throw (Exception); 25 | static void release_instance(); 26 | 27 | Video *video; 28 | ResourcesManager *resources_manager; 29 | EventsManager *events_manager; 30 | AudioManagerWrapper *audio_manager; 31 | 32 | Canvas *canvas; 33 | Camera *camera; 34 | AudioManagerMusic *music; 35 | AudioManagerSfx *sfx; 36 | 37 | string m_settings_path; 38 | 39 | private: 40 | Environment(); 41 | void init() throw (Exception); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /engine/include/core/eventsmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que gerencia os eventos de input do jogo. 3 | * 4 | * Autor: Carlos Oliveira 5 | * Data: 17/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef EVENTS_MANAGER_H 9 | #define EVENTS_MANAGER_H 10 | 11 | #include 12 | 13 | using std::unique_ptr; 14 | 15 | class Listener; 16 | 17 | class EventsManager 18 | { 19 | public: 20 | EventsManager(); 21 | ~EventsManager(); 22 | 23 | void dispatch_pending_events(); 24 | 25 | void register_listener(Listener *listener); 26 | void unregister_listener(Listener *listener); 27 | 28 | private: 29 | class Impl; 30 | unique_ptr m_impl; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /engine/include/core/exception.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma exceção. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 01/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef EXCEPTION_H 9 | #define EXCEPTION_H 10 | 11 | #include 12 | 13 | using std::string; 14 | 15 | class Exception 16 | { 17 | public: 18 | Exception(const string& message) 19 | : m_message(message) {} 20 | 21 | string message() const { return m_message; } 22 | 23 | private: 24 | string m_message; 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /engine/include/core/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma fonte. 3 | * 4 | * Autor: Carlos Oliveira 5 | * Data: 18/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef FONT_H 9 | #define FONT_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #include "exception.h" 16 | #include "color.h" 17 | 18 | using std::string; 19 | using std::unique_ptr; 20 | 21 | class Font 22 | { 23 | public: 24 | typedef enum { NORMAL, BOLD, ITALIC } Style; 25 | ~Font(); 26 | 27 | static Font * from_file(const string& path) throw (Exception); 28 | 29 | TTF_Font * font() const; 30 | int size() const; 31 | Style style() const; 32 | 33 | void set_size(int size); 34 | void set_style(Style style); 35 | 36 | private: 37 | class Impl; 38 | unique_ptr m_impl; 39 | 40 | Font(TTF_Font *font, const string& path, int size = 20); 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /engine/include/core/game.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 26/03/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef GAME_H 9 | #define GAME_H 10 | 11 | #include "core/level.h" 12 | #include "core/listener.h" 13 | #include "core/exception.h" 14 | 15 | #include 16 | 17 | using std::string; 18 | 19 | class Environment; 20 | class SystemEvent; 21 | class KeyboardEvent; 22 | 23 | class Game : public Listener 24 | { 25 | public: 26 | Game(const string& id); 27 | virtual ~Game(); 28 | 29 | void init(const string& title, int w, int h, double scale = 1.0, bool fullscreen = false, 30 | int volume = 50) throw (Exception); 31 | 32 | void init(const string& settings) throw (Exception); 33 | void run(); 34 | 35 | bool on_event(const SystemEvent& event); 36 | bool on_event(const KeyboardEvent& event); 37 | 38 | protected: 39 | string m_id; 40 | Level *m_level; 41 | bool m_done; 42 | Environment *env; 43 | 44 | virtual Level * load_level(const string& id); 45 | 46 | unsigned long update_timestep() const; 47 | void process_input(); 48 | void update_screen(); 49 | void delay(unsigned long ms); 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /engine/include/core/image.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma imagem. 3 | * 4 | * Autor: Jefferson Xavier 5 | * Data: 16/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef IMAGE_H 9 | #define IMAGE_H 10 | 11 | #include "core/object.h" 12 | 13 | #include 14 | 15 | using std::unique_ptr; 16 | 17 | class Image : public Object 18 | { 19 | public: 20 | Image(Object *parent, const string& image); 21 | ~Image(); 22 | 23 | private: 24 | class Impl; 25 | unique_ptr m_impl; 26 | 27 | void draw_self(); 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /engine/include/core/joystickevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um evento de controle. 3 | * 4 | * Autor: Simiao Carvalho 5 | * Data: 26/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef JOYSTICKEVENT_H 9 | #define JOYSTICKEVENT_H 10 | 11 | #include 12 | 13 | class JoyStickEvent 14 | { 15 | public: 16 | typedef enum {PRESSED, RELEASED} State; 17 | 18 | typedef enum 19 | { 20 | X, // X no controle de PS4 - 0 21 | CIRCLE, // Bola no controle de PS4 - 1 22 | SQUARE, // Quadrado no controle de PS4 - 2 23 | TRIANGLE, // Triângulo no controle de PS4 - 3 24 | 25 | SHARE, // SHARE no controle de PS4 - 4 26 | HOME, // Botão PS no controle de PS4 - 5 27 | START, // Options no controle de PS4 - 6 28 | 29 | L3, // Botão do analógico esquerdo - 7 30 | R3, // Botão do analógico direito - 8 31 | 32 | L1, // L1 (PS4) - 9 33 | R1, // R1 (PS4)- 10 34 | 35 | UP, // D-Pad up - 11 36 | DOWN, // D-Pad down - 12 37 | LEFT, // D-Pad down - 13 38 | RIGHT, // D-Pad Rigth - 14 39 | } Button; 40 | 41 | JoyStickEvent(State state, Button button); 42 | 43 | State state() const; 44 | Button button() const; 45 | 46 | static JoyStickEvent from_SDL(const SDL_Event& event); 47 | 48 | private: 49 | State m_state; 50 | Button m_button; 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /engine/include/core/keyboardevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um evento de teclado. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 22/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef KEYBOARDEVENT_H 9 | #define KEYBOARDEVENT_H 10 | 11 | #include 12 | 13 | class KeyboardEvent 14 | { 15 | public: 16 | typedef enum {PRESSED, RELEASED} State; 17 | 18 | typedef 19 | enum { 20 | BACKSPACE, TAB, CLEAR, RETURN, PAUSE, ESCAPE, SPACE, EXCLAIM, 21 | DOUBLE_QUOTE, HASH, DOLLAR, AMPERSAND, QUOTE, LEFT_PARENTESIS, 22 | RIGHT_PARENTESIS, ASTERISK, PLUS, COMMA, MINUS, PERIOD, SLASH, ZERO, 23 | ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, COLON, SEMICOLON, 24 | LESS, EQUALS, GREATER, QUESTION, AT, LEFT_BRACKET, BACKSLASH, 25 | RIGHT_BRACKET, CARET, UNDERSCORE, BACKQUOTE, DELETE, A, B, C, D, E, F, 26 | G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, LEFT, 27 | RIGHT, UP, DOWN, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12 28 | } Key; 29 | 30 | typedef enum {NONE, SHIFT, CONTROL, ALT, CAPS} Modifier; 31 | 32 | KeyboardEvent(State state, Key key, Modifier modifier); 33 | 34 | State state() const; 35 | Key key() const; 36 | Modifier modifier() const; 37 | 38 | static KeyboardEvent from_SDL(const SDL_Event& event); 39 | 40 | private: 41 | State m_state; 42 | Key m_key; 43 | Modifier m_modifier; 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /engine/include/core/level.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um nível no mundo do jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef LEVEL_H 9 | #define LEVEL_H 10 | 11 | #include "object.h" 12 | #include "environment.h" 13 | 14 | #include 15 | 16 | using std::string; 17 | 18 | class Level : public Object 19 | { 20 | public: 21 | Level(const string& id, const string& next = ""); 22 | 23 | string next() const; 24 | bool finished() const; 25 | 26 | void finish(); 27 | void set_next(const string& next); 28 | 29 | private: 30 | string m_next; 31 | bool m_done; 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /engine/include/core/line.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma reta no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef LINE_H 9 | #define LINE_H 10 | 11 | #include "point.h" 12 | 13 | class Line 14 | { 15 | public: 16 | Line(const Point& a = Point(), const Point& b = Point()); 17 | 18 | const Point& a() const; 19 | const Point& b() const; 20 | 21 | void set_a(const Point& a); 22 | void set_b(const Point& b); 23 | void set(const Point& a, const Point& b); 24 | 25 | private: 26 | Point m_a, m_b; 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /engine/include/core/listener.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um ouvinte de eventos. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 25/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef LISTENER_H 9 | #define LISTENER_H 10 | 11 | #include 12 | 13 | using std::unique_ptr; 14 | 15 | class SystemEvent; 16 | class KeyboardEvent; 17 | class JoyStickEvent; 18 | class MouseButtonEvent; 19 | class MouseMotionEvent; 20 | 21 | class Listener 22 | { 23 | public: 24 | Listener(); 25 | ~Listener(); 26 | 27 | virtual bool on_event(const SystemEvent& event); 28 | virtual bool on_event(const KeyboardEvent& event); 29 | virtual bool on_event(const JoyStickEvent& event); 30 | virtual bool on_event(const MouseButtonEvent& event); 31 | virtual bool on_event(const MouseMotionEvent& event); 32 | 33 | bool active() const; 34 | void set_active(bool active = true); 35 | 36 | private: 37 | class Impl; 38 | unique_ptr m_impl; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /engine/include/core/mousebuttonevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um evento de botão do mouse. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 22/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef MOUSE_BUTTON_EVENT_H 9 | #define MOUSE_BUTTON_EVENT_H 10 | 11 | #include 12 | 13 | class MouseButtonEvent 14 | { 15 | public: 16 | typedef enum {PRESSED, RELEASED} ButtonState; 17 | typedef enum { LEFT, RIGHT, MIDDLE } Button; 18 | 19 | MouseButtonEvent(ButtonState state, Button button, double x, double y); 20 | 21 | ButtonState state() const; 22 | Button button() const; 23 | double x() const; 24 | double y() const; 25 | 26 | static MouseButtonEvent from_SDL(const SDL_Event& event); 27 | 28 | private: 29 | ButtonState m_state; 30 | Button m_button; 31 | double m_x, m_y; 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /engine/include/core/mousemotionevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um evento de movimentação do mouse. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 25/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef MOUSE_MOTION_EVENT_H 9 | #define MOUSE_MOTION_EVENT_H 10 | 11 | #include 12 | 13 | class MouseMotionEvent 14 | { 15 | public: 16 | typedef enum { DOWN, UP } MotionState; 17 | typedef enum { LEFT, RIGHT, MIDDLE } Button; 18 | 19 | MouseMotionEvent(int x, int y, int x_rel, int y_rel, MotionState left, 20 | MotionState right, MotionState middle); 21 | 22 | MotionState state(Button button) const; 23 | int x() const; 24 | int y() const; 25 | int x_rel() const; 26 | int y_rel() const; 27 | 28 | static MouseMotionEvent from_SDL(const SDL_Event& event); 29 | 30 | private: 31 | int m_x, m_y; 32 | int m_x_rel, m_y_rel; 33 | MotionState m_buttons[3]; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /engine/include/core/music.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um arquivo de musica. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef MUSIC_H 9 | #define MUSIC_H 10 | 11 | #include 12 | #include 13 | #include 14 | #include "core/exception.h" 15 | 16 | using std::string; 17 | using std::unique_ptr; 18 | 19 | class Music 20 | { 21 | public: 22 | explicit Music(Mix_Music* const music); 23 | ~Music(); 24 | 25 | Mix_Music * mix_music() const; 26 | string path() const; 27 | 28 | static Music * from_file(const string& path) throw (Exception); 29 | 30 | private: 31 | class Impl; 32 | unique_ptr m_impl; 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /engine/include/core/object.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um objeto do mundo do jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef OBJECT_H 9 | #define OBJECT_H 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | using std::list; 16 | using std::string; 17 | using std::unique_ptr; 18 | 19 | class Rect; 20 | 21 | typedef string ObjectID; 22 | typedef string ActionID; 23 | typedef string MessageID; 24 | typedef string Parameters; 25 | 26 | class Object 27 | { 28 | public: 29 | Object(Object *parent = nullptr, ObjectID id = "", double x = 0, 30 | double y = 0, double w = 1, double h = 1); 31 | virtual ~Object(); 32 | 33 | Object * parent() const; 34 | const list& children() const; 35 | ObjectID id() const; 36 | 37 | double x() const; 38 | double y() const; 39 | double w() const; 40 | double h() const; 41 | 42 | const Rect& bounding_box() const; 43 | 44 | bool visible() const; 45 | 46 | void set_x(double x); 47 | void set_y(double y); 48 | void set_w(double w); 49 | void set_h(double h); 50 | 51 | void set_visible(bool visible = true); 52 | 53 | void set_position(double x, double y); 54 | void set_dimensions(double w, double h); 55 | void set_parent(Object *parent); 56 | 57 | typedef enum { NONE, LEFT, CENTER, RIGHT, TOP, MIDDLE, BOTTOM } 58 | Alignment; 59 | 60 | void align_to(const Object* object, Alignment xaxis, Alignment yaxis); 61 | 62 | void add_child(Object *child); 63 | void remove_child(Object *child); 64 | 65 | bool send_message(Object *receiver, MessageID id, Parameters parameters); 66 | virtual bool on_message(Object *sender, MessageID id, Parameters p); 67 | 68 | void add_observer(Object *observer); 69 | void remove_observer(Object *observer); 70 | 71 | void notify(ActionID action, Parameters parameters); 72 | 73 | void update(unsigned long elapsed); 74 | void draw(); 75 | 76 | private: 77 | class Impl; 78 | unique_ptr m_impl; 79 | 80 | virtual void update_self(unsigned long elapsed); 81 | virtual void draw_self(); 82 | }; 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /engine/include/core/point.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um ponto no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef POINT_H 9 | #define POINT_H 10 | 11 | class Point 12 | { 13 | public: 14 | Point(double x = 0, double y = 0); 15 | 16 | double x() const; 17 | double y() const; 18 | 19 | void set_x(double x); 20 | void set_y(double y); 21 | void set(double x, double y); 22 | 23 | private: 24 | double m_x, m_y; 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /engine/include/core/rect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um retângulo no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef RECT_H 9 | #define RECT_H 10 | 11 | class Rect 12 | { 13 | public: 14 | Rect(double x = 0, double y = 0, double w = 0, double h = 0); 15 | 16 | double x() const; 17 | double y() const; 18 | double w() const; 19 | double h() const; 20 | 21 | void set_x(double x); 22 | void set_y(double y); 23 | void set_w(double w); 24 | void set_h(double h); 25 | 26 | void set_position(double x, double y); 27 | void set_dimensions(double w, double h); 28 | 29 | bool contains(double x, double y) const; 30 | Rect intersection(const Rect& r) const; 31 | 32 | protected: 33 | double m_x, m_y; 34 | double m_w, m_h; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /engine/include/core/resourcesmanager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um gerenciador de recursos reutilizáveis. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef RESOURCES_MANAGER_H 9 | #define RESOURCES_MANAGER_H 10 | 11 | #include 12 | #include 13 | 14 | #include "exception.h" 15 | 16 | using std::map; 17 | using std::shared_ptr; 18 | 19 | class Texture; 20 | class Font; 21 | class Music; 22 | class SoundEffect; 23 | class Bitmap; 24 | class Settings; 25 | 26 | class ResourcesManager 27 | { 28 | public: 29 | shared_ptr get_texture(const string& id) throw (Exception); 30 | shared_ptr get_font(const string& id) throw (Exception); 31 | shared_ptr get_music(const string& id) throw (Exception); 32 | shared_ptr get_sound_effect(const string& id) throw (Exception); 33 | shared_ptr get_bitmap(const string& id) throw (Exception); 34 | shared_ptr get_settings(const string& id) throw (Exception); 35 | 36 | void scale(double k); 37 | 38 | private: 39 | map < string, shared_ptr > m_textures; 40 | map < string, shared_ptr > m_fonts; 41 | map < string, shared_ptr > m_musics; 42 | map < string, shared_ptr > m_sound_effects; 43 | map < string, shared_ptr > m_bitmaps; 44 | map < string, shared_ptr > m_settings; 45 | 46 | shared_ptr acquire_texture(const string& id) throw (Exception); 47 | shared_ptr acquire_font(const string& id) throw (Exception); 48 | shared_ptr acquire_music(const string& id) throw (Exception); 49 | shared_ptr acquire_sound_effect(const string& id) throw (Exception); 50 | shared_ptr acquire_bitmap(const string& id) throw (Exception); 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /engine/include/core/settings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um conjunto de configurações. 3 | * 4 | * Cada configuração é representada por um par/valor. 5 | * 6 | * Autor: Edson Alves 7 | * Data: 20/05/2015 8 | * Licença: LGPL. Sem copyright. 9 | */ 10 | #ifndef SETTINGS_H 11 | #define SETTINGS_H 12 | 13 | #include "core/exception.h" 14 | 15 | #include 16 | #include 17 | 18 | using std::map; 19 | using std::ostringstream; 20 | using std::istringstream; 21 | 22 | class Settings 23 | { 24 | public: 25 | static Settings * from_file(const string& path) throw (Exception); 26 | void save(const string& path) throw (Exception); 27 | 28 | map< string, map > sections() const; 29 | 30 | template 31 | T read(const string& section, const string& name, const T& default_value) 32 | const; 33 | 34 | template 35 | void write(const string& section, const string& name, const T& value); 36 | 37 | private: 38 | map< string, map > m_sections; 39 | }; 40 | 41 | template T 42 | Settings::read(const string& section, const string& name, 43 | const T& default_value) const 44 | { 45 | auto it = m_sections.find(section); 46 | 47 | if (it != m_sections.end()) 48 | { 49 | auto jt = it->second.find(name); 50 | 51 | if (jt != it->second.end()) 52 | { 53 | istringstream iss(jt->second); 54 | T value; 55 | 56 | iss >> value; 57 | 58 | return value; 59 | } 60 | } 61 | 62 | return default_value; 63 | } 64 | 65 | template void 66 | Settings::write(const string& section, const string& name, const T& value) 67 | { 68 | ostringstream os; 69 | os << value; 70 | 71 | auto it = m_sections.find(section); 72 | 73 | if (it == m_sections.end()) 74 | { 75 | m_sections[section] = map(); 76 | } 77 | 78 | m_sections[section][name] = os.str(); 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /engine/include/core/soundeffect.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um arquivo de efeito sonoro. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef SOUNDEFFECT_H 9 | #define SOUNDEFFECT_H 10 | 11 | #include 12 | #include 13 | #include 14 | #include "core/exception.h" 15 | 16 | using std::string; 17 | using std::unique_ptr; 18 | 19 | class SoundEffect 20 | { 21 | public: 22 | explicit SoundEffect(Mix_Chunk* const soundeffect); 23 | ~SoundEffect(); 24 | 25 | Mix_Chunk* mix_chunk() const; 26 | int channel() const; 27 | void set_channel(const int channel); 28 | 29 | static SoundEffect * from_file(const string& path) throw (Exception); 30 | 31 | private: 32 | class Impl; 33 | unique_ptr m_impl; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /engine/include/core/sprite.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um sprite do jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 05/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef SPRITE_H 9 | #define SPRITE_H 10 | 11 | #include "core/object.h" 12 | #include "core/animation.h" 13 | #include "core/listener.h" 14 | 15 | #include 16 | #include 17 | 18 | using std::map; 19 | using std::pair; 20 | 21 | class KeyboardEvent; 22 | class MouseButtonEvent; 23 | class MouseMotionEvent; 24 | class JoystickEvent; 25 | 26 | class SpriteState 27 | { 28 | public: 29 | virtual void update(unsigned long elapsed) = 0; 30 | virtual void draw() = 0; 31 | virtual void enter(int from) = 0; 32 | virtual void leave(int to) = 0; 33 | 34 | virtual bool on_event(const KeyboardEvent&) { return false; } 35 | virtual bool on_event(const MouseButtonEvent&) { return false; } 36 | virtual bool on_event(const MouseMotionEvent&) { return false; } 37 | virtual bool on_event(const JoyStickEvent&) { return false; } 38 | }; 39 | 40 | class Sprite : public Object, public Listener 41 | { 42 | public: 43 | Sprite(Object *parent, ObjectID id); 44 | ~Sprite(); 45 | 46 | void report_event(int event); 47 | void change_state(int to, int from); 48 | 49 | void add_state(int id, SpriteState *state); 50 | void add_transition(int event, int from, int to); 51 | 52 | SpriteState * state() const; 53 | 54 | bool on_event(const KeyboardEvent& event); 55 | bool on_event(const MouseButtonEvent& event); 56 | bool on_event(const MouseMotionEvent& event); 57 | bool on_event(const JoyStickEvent& event); 58 | 59 | private: 60 | class Impl; 61 | unique_ptr m_impl; 62 | 63 | void update_self(unsigned long elapsed); 64 | void draw_self(); 65 | }; 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /engine/include/core/systemevent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa um evento do sistema operacional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 22/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef SYSTEM_EVENT_H 9 | #define SYSTEM_EVENT_H 10 | 11 | #include 12 | 13 | class SystemEvent 14 | { 15 | public: 16 | typedef enum { UNKNOWN, QUIT } Type; 17 | 18 | SystemEvent(Type type); 19 | 20 | Type type() const; 21 | 22 | static SystemEvent from_SDL(const SDL_Event& event); 23 | 24 | private: 25 | Type m_type; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /engine/include/core/text.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma texto. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 14/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef TEXT_H 9 | #define TEXT_H 10 | 11 | #include "core/object.h" 12 | #include "core/color.h" 13 | 14 | #include 15 | 16 | using std::unique_ptr; 17 | 18 | class Text : public Object 19 | { 20 | public: 21 | Text(Object *parent, const string& text, const Color& color = Color::BLACK); 22 | ~Text(); 23 | 24 | private: 25 | class Impl; 26 | unique_ptr m_impl; 27 | 28 | void draw_self(); 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /engine/include/core/texture.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa uma textura. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef TEXTURE_H 9 | #define TEXTURE_H 10 | 11 | #include "core/exception.h" 12 | 13 | #include 14 | #include 15 | 16 | using std::pair; 17 | using std::string; 18 | using std::unique_ptr; 19 | 20 | class Texture 21 | { 22 | public: 23 | Texture(void *data, int w, int h); 24 | ~Texture(); 25 | 26 | int w() const; 27 | int h() const; 28 | void * data() const; 29 | 30 | static Texture * from_file(const string& path) throw (Exception); 31 | 32 | void scale(double k); 33 | pair size() const; 34 | 35 | private: 36 | class Impl; 37 | unique_ptr m_impl; 38 | }; 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /engine/include/core/video.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa o módulo de vídeo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 06/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef VIDEO_H 9 | #define VIDEO_H 10 | 11 | #include 12 | #include 13 | 14 | #include "exception.h" 15 | 16 | using std::string; 17 | using std::pair; 18 | 19 | class Canvas; 20 | class Camera; 21 | 22 | class Video 23 | { 24 | public: 25 | Video(); 26 | ~Video(); 27 | 28 | void init() throw (Exception); 29 | 30 | void set_resolution(int w, int h, double scale = 1) throw (Exception); 31 | void set_fullscreen(bool fullscreen = true) throw (Exception); 32 | void set_window_name(const string& name); 33 | 34 | bool fullscreen() const; 35 | pair resolution() const; 36 | Canvas * canvas() const; 37 | Camera * camera() const; 38 | 39 | private: 40 | SDL_Window *m_window; 41 | SDL_Renderer *m_renderer; 42 | Canvas *m_canvas; 43 | Camera *m_camera; 44 | 45 | int m_w, m_h; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /engine/include/util/button.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Exemplo de um objeto: uma botão clicável. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 29/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef BUTTON_H 9 | #define BUTTON_H 10 | 11 | #include "core/color.h" 12 | #include "core/object.h" 13 | #include "core/listener.h" 14 | 15 | #include 16 | 17 | using std::unique_ptr; 18 | 19 | class Button : public Object, public Listener 20 | { 21 | public: 22 | Button(Object *parent, ObjectID id, double w, double h); 23 | Button(Object *parent, ObjectID id, const string& idle_image_id, 24 | const string& active_image_id); 25 | ~Button(); 26 | 27 | static ActionID clickedID; 28 | 29 | bool on_event(const MouseButtonEvent& event); 30 | bool on_event(const MouseMotionEvent& event); 31 | 32 | void set_text(const string& text, const Color& color = Color::BLACK); 33 | void set_color(const Color& idle, const Color& active); 34 | void set_border(int thickness = 1, const Color& color = Color::BLACK); 35 | 36 | private: 37 | class Impl; 38 | unique_ptr m_impl; 39 | 40 | void draw_self(); 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /engine/include/util/frontend.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Exemplo de um nível: uma tela de frontend. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef FRONT_END_H 9 | #define FRONT_END_H 10 | 11 | #include "core/level.h" 12 | #include "core/listener.h" 13 | 14 | #include 15 | 16 | using std::unique_ptr; 17 | 18 | class FrontEnd : public Level, public Listener 19 | { 20 | public: 21 | FrontEnd(const string& id, const string& next, const string& image, 22 | unsigned long duration = 3000, const Color& background = Color::BLACK); 23 | ~FrontEnd(); 24 | 25 | bool on_event(const MouseButtonEvent& event); 26 | bool on_event(const KeyboardEvent& event); 27 | bool on_event(const JoyStickEvent& event); 28 | 29 | private: 30 | class Impl; 31 | unique_ptr m_impl; 32 | 33 | void update_self(unsigned long elapsed); 34 | void draw_self(); 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /engine/lint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -o posix 3 | 4 | LINE_LENGTH=80 5 | EXTENSIONS=h,cpp 6 | VERBOSITY=0 7 | 8 | # FILTERS+=-build/class, 9 | # FILTERS+=-build/c++11, 10 | # FILTERS+=-build/deprecated, 11 | # FILTERS+=-build/endif_comment, 12 | # FILTERS+=-build/explicit_make_pair, 13 | # FILTERS+=-build/forward_decl, 14 | FILTERS+=-build/header_guard, 15 | # FILTERS+=-build/include, 16 | # FILTERS+=-build/include_alpha, 17 | # FILTERS+=-build/include_order, 18 | # FILTERS+=-build/include_what_you_use, 19 | # FILTERS+=-build/namespaces, 20 | # FILTERS+=-build/printf_format, 21 | # FILTERS+=-build/storage_class, 22 | FILTERS+=-legal/copyright, 23 | # FILTERS+=-readability/alt_tokens, 24 | FILTERS+=-readability/braces, 25 | # FILTERS+=-readability/casting, 26 | # FILTERS+=-readability/check, 27 | # FILTERS+=-readability/constructors, 28 | # FILTERS+=-readability/fn_size, 29 | # FILTERS+=-readability/function, 30 | # FILTERS+=-readability/inheritance, 31 | FILTERS+=-readability/multiline_comment, 32 | # FILTERS+=-readability/multiline_string, 33 | # FILTERS+=-readability/namespace, 34 | # FILTERS+=-readability/nolint, 35 | # FILTERS+=-readability/nul, 36 | # FILTERS+=-readability/strings, 37 | # FILTERS+=-readability/todo, 38 | # FILTERS+=-readability/utf8, 39 | # FILTERS+=-runtime/arrays, 40 | # FILTERS+=-runtime/casting, 41 | # FILTERS+=-runtime/explicit, 42 | # FILTERS+=-runtime/int, 43 | # FILTERS+=-runtime/init, 44 | # FILTERS+=-runtime/invalid_increment, 45 | # FILTERS+=-runtime/member_string_references, 46 | # FILTERS+=-runtime/memset, 47 | # FILTERS+=-runtime/indentation_namespace, 48 | # FILTERS+=-runtime/operator, 49 | # FILTERS+=-runtime/printf, 50 | # FILTERS+=-runtime/printf_format, 51 | # FILTERS+=-runtime/references, 52 | # FILTERS+=-runtime/string, 53 | # FILTERS+=-runtime/threadsafe_fn, 54 | # FILTERS+=-runtime/vlog, 55 | FILTERS+=-whitespace/blank_line, 56 | FILTERS+=-whitespace/braces, 57 | # FILTERS+=-whitespace/comma, 58 | FILTERS+=-whitespace/comments, 59 | # FILTERS+=-whitespace/empty_conditional_body, 60 | # FILTERS+=-whitespace/empty_loop_body, 61 | # FILTERS+=-whitespace/end_of_line, 62 | # FILTERS+=-whitespace/ending_newline, 63 | # FILTERS+=-whitespace/forcolon, 64 | # FILTERS+=-whitespace/indent, 65 | # FILTERS+=-whitespace/line_length, 66 | FILTERS+=-whitespace/newline, 67 | # FILTERS+=-whitespace/operators, 68 | # FILTERS+=-whitespace/parens, 69 | # FILTERS+=-whitespace/semicolon, 70 | FILTERS+=-whitespace/tab, 71 | # FILTERS+=-whitespace/todo, 72 | 73 | # src/ 74 | touch cpplint_src.log 75 | ./cpplint.py --linelength=${LINE_LENGTH} --extensions=${EXTENSIONS} --verbose=${VERBOSITY}\ 76 | --filter=${FILTERS} src/*.* 2> cpplint_src.log 77 | 78 | # include/ 79 | touch cpplint_include.log 80 | ./cpplint.py --linelength=${LINE_LENGTH} --extensions=${EXTENSIONS} --verbose=${VERBOSITY}\ 81 | --filter=${FILTERS} include/*/*.* 2> cpplint_include.log 82 | -------------------------------------------------------------------------------- /engine/res/dll/windows.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/dll/windows.zip -------------------------------------------------------------------------------- /engine/res/fonts/FLATS.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/fonts/FLATS.ttf -------------------------------------------------------------------------------- /engine/res/images/hexagon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/images/hexagon.png -------------------------------------------------------------------------------- /engine/res/images/idle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/images/idle.png -------------------------------------------------------------------------------- /engine/res/images/image_credits.txt: -------------------------------------------------------------------------------- 1 | Idle and running sprites from http://opengameart.org/content/bevouliin-free-game-sprites-crocodile-mascot-running-and-jumping-boy-game-character (http://bevouliin.com/) 2 | -------------------------------------------------------------------------------- /engine/res/images/running.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/images/running.png -------------------------------------------------------------------------------- /engine/res/images/spiral.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/images/spiral.png -------------------------------------------------------------------------------- /engine/res/images/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fgagamedev/IJE/03f235ee56424f084e27938047a9cd47d27047ea/engine/res/images/star.png -------------------------------------------------------------------------------- /engine/src/animation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Animation. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 05/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/animation.h" 9 | 10 | #include "core/rect.h" 11 | #include "core/texture.h" 12 | #include "core/environment.h" 13 | 14 | class Animation::Impl 15 | { 16 | public: 17 | Impl(const string& id, double x_, double y_, double w_, double h_, int f, 18 | unsigned long s, bool l_) 19 | : x(x_), y(y_), w(w_), h(h_), frames(f), speed(s), loop(l_), 20 | done(false), last(0), frame(0) 21 | { 22 | Environment *env = Environment::get_instance(); 23 | texture = env->resources_manager->get_texture(id); 24 | } 25 | 26 | void reset() 27 | { 28 | last = frame = 0; 29 | } 30 | 31 | double x, y, w, h; 32 | int frames; 33 | unsigned long speed; 34 | bool loop, done; 35 | unsigned long last; 36 | int frame; 37 | shared_ptr texture; 38 | }; 39 | 40 | Animation::Animation(const string& texture, double x, double y, double w, 41 | double h, int frames, unsigned long speed_in_ms, bool loop) 42 | : m_impl(new Animation::Impl(texture, x, y, w, h, frames, speed_in_ms, loop)) 43 | { 44 | } 45 | 46 | Animation::~Animation() 47 | { 48 | } 49 | 50 | bool 51 | Animation::is_done() const 52 | { 53 | return m_impl->done; 54 | } 55 | 56 | void 57 | Animation::update(unsigned long elapsed) 58 | { 59 | if (not m_impl->last) 60 | { 61 | m_impl->last = elapsed; 62 | } 63 | 64 | if (elapsed - m_impl->last > m_impl->speed) 65 | { 66 | ++m_impl->frame; 67 | m_impl->last += m_impl->speed; 68 | 69 | if (m_impl->loop) 70 | { 71 | m_impl->frame %= m_impl->frames; 72 | } else if (m_impl->frame == m_impl->frames) 73 | { 74 | --m_impl->frame; 75 | m_impl->done = true; 76 | } 77 | } 78 | } 79 | 80 | void 81 | Animation::draw(double x, double y) 82 | { 83 | if (m_impl->done) 84 | { 85 | return; 86 | } 87 | 88 | Rect clip { m_impl->x + m_impl->frame * m_impl->w, m_impl->y, m_impl->w, 89 | m_impl->h 90 | }; 91 | 92 | Environment *env = Environment::get_instance(); 93 | env->canvas->draw(m_impl->texture.get(), clip, x, y, clip.w(), clip.h()); 94 | } 95 | 96 | double 97 | Animation::w() const 98 | { 99 | return m_impl->w; 100 | } 101 | 102 | double 103 | Animation::h() const 104 | { 105 | return m_impl->h; 106 | } 107 | 108 | void 109 | Animation::set_row(int row) 110 | { 111 | int y = row*m_impl->h; 112 | 113 | if (m_impl->y != y) 114 | { 115 | m_impl->frame = 0; 116 | } 117 | 118 | m_impl->y = y; 119 | } 120 | 121 | void 122 | Animation::reset() 123 | { 124 | m_impl->reset(); 125 | } 126 | -------------------------------------------------------------------------------- /engine/src/audiomanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe AudioManager. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/audiomanager.h" 9 | #include 10 | #include 11 | #include 12 | #include "core/music.h" 13 | #include "core/soundeffect.h" 14 | #include "core/environment.h" 15 | 16 | using std::cout; 17 | using std::endl; 18 | 19 | namespace 20 | { 21 | static constexpr auto k_all_channels = -1; 22 | static constexpr auto k_any_channel = -1; 23 | } // namespace 24 | 25 | // ----------------------------------------------------------- // 26 | // Music Handling 27 | // ----------------------------------------------------------- // 28 | template<> 29 | AudioManager::AudioManager() 30 | { 31 | SDL_InitSubSystem(SDL_INIT_AUDIO); 32 | Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096); 33 | Mix_AllocateChannels(4000); 34 | } 35 | 36 | template <> 37 | void 38 | AudioManager::play(const string& path, const int times) 39 | { 40 | assert((times == k_infinite_loop || times >= 1) && "Must be k_infinite_loop or >= 1."); 41 | 42 | const auto music = Environment::get_instance()->resources_manager->get_music(path); 43 | assert(music != nullptr && "Shouldn't be playing null music."); 44 | 45 | const auto k_played_music = (Mix_PlayMusic(music->mix_music(), times) == 0); 46 | if (not k_played_music) 47 | { 48 | cout << "Couldn't play music (" << path << "). " << Mix_GetError() << endl; 49 | } 50 | } 51 | 52 | template <> 53 | void 54 | AudioManager::set_volume(const int percent) 55 | { 56 | /// @todo Decide whether the <= 100 assertion is necessary. 57 | // No need to check for percent being above 100, SDL_Mixer already caps the volume to 58 | // MIX_MAX_VOLUME. 59 | assert(percent >= 0 && "Must be >= 0"); 60 | assert(percent <= 100 && "Must be <= 100"); 61 | 62 | const auto k_volume_value = percent * MIX_MAX_VOLUME/100; 63 | Mix_VolumeMusic(k_volume_value); 64 | } 65 | 66 | template <> 67 | void 68 | AudioManager::pause() 69 | { 70 | Mix_PauseMusic(); 71 | } 72 | 73 | template <> 74 | void 75 | AudioManager::resume() 76 | { 77 | Mix_ResumeMusic(); 78 | } 79 | 80 | template <> 81 | void 82 | AudioManager::stop() 83 | { 84 | Mix_HaltMusic(); 85 | } 86 | 87 | template <> 88 | void 89 | AudioManager::fade_out(const double seconds) 90 | { 91 | const auto k_milliseconds = static_cast(seconds*1000); 92 | const auto k_faded = (Mix_FadeOutMusic(k_milliseconds) == 1); 93 | if (not k_faded) 94 | { 95 | cout << "Could not fade music. Probably no music playing/already faded." << endl; 96 | } 97 | } 98 | 99 | template <> 100 | bool AudioManager::faded_out() 101 | { 102 | const auto k_fading = (Mix_FadingMusic() == MIX_FADING_OUT); 103 | const auto k_playing = (Mix_PlayingMusic() == 1); 104 | 105 | return not k_fading and not k_playing; 106 | } 107 | 108 | // ----------------------------------------------------------- // 109 | // Sound Effects Handling 110 | // ----------------------------------------------------------- // 111 | template<> 112 | AudioManager::AudioManager() 113 | { 114 | } 115 | 116 | template <> 117 | void 118 | AudioManager::play(const string& path, const int times) 119 | { 120 | assert((times == k_infinite_loop || times >= 1) && "Must be k_infinite_loop or >= 1."); 121 | 122 | const auto sound_effect = 123 | Environment::get_instance()->resources_manager->get_sound_effect(path); 124 | 125 | assert(sound_effect != nullptr && "Shouldn't be playing a null sound effect."); 126 | 127 | const auto k_played_channel = Mix_PlayChannel(k_any_channel, sound_effect->mix_chunk(), 128 | (times - 1)); 129 | if (k_played_channel == -1) 130 | { 131 | cout << "Failed to play sound effect for any channel (" << k_played_channel << 132 | "). " << Mix_GetError() << endl; 133 | } 134 | } 135 | 136 | template <> 137 | void 138 | AudioManager::set_volume(const int percent) 139 | { 140 | /// @todo Decide whether the <= 100 assertion is necessary. 141 | // No need to check for percent being above 100, SDL_Mixer already caps the volume to 142 | // MIX_MAX_VOLUME. 143 | assert(percent >= 0 && "Must be >= 0"); 144 | assert(percent <= 100 && "Must be <= 100"); 145 | 146 | const auto k_volume_value = percent * MIX_MAX_VOLUME/100; 147 | Mix_Volume(k_all_channels, k_volume_value); 148 | } 149 | 150 | template <> 151 | void 152 | AudioManager::pause() 153 | { 154 | Mix_Pause(k_all_channels); 155 | } 156 | 157 | template <> 158 | void 159 | AudioManager::resume() 160 | { 161 | Mix_Resume(k_all_channels); 162 | } 163 | 164 | template <> 165 | void 166 | AudioManager::stop() 167 | { 168 | Mix_HaltChannel(k_all_channels); 169 | } 170 | 171 | template <> 172 | void 173 | AudioManager::fade_out(const double seconds) 174 | { 175 | const auto k_milliseconds = static_cast(seconds*1000); 176 | Mix_FadeOutChannel(k_all_channels, k_milliseconds); 177 | } 178 | 179 | // template <> 180 | // bool AudioManager::faded_out() 181 | // { 182 | // return false; 183 | // } 184 | -------------------------------------------------------------------------------- /engine/src/audiomanagerwrapper.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe AudioManagerWrapper. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 18/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/audiomanagerwrapper.h" 9 | #include 10 | #include 11 | #include 12 | 13 | using std::cout; 14 | using std::endl; 15 | 16 | AudioManagerWrapper::AudioManagerWrapper() 17 | : sfx_manager{nullptr}, music_manager{nullptr} 18 | { 19 | open_audio(); 20 | } 21 | 22 | AudioManagerWrapper::~AudioManagerWrapper() 23 | { 24 | close_audio(); 25 | delete music_manager; 26 | delete sfx_manager; 27 | } 28 | 29 | void 30 | AudioManagerWrapper::init() throw (Exception) 31 | { 32 | sfx_manager = new AudioManagerSfx(); 33 | 34 | if (not sfx_manager) 35 | { 36 | throw Exception("Out of memory for a new AudioManagerSfx"); 37 | } 38 | 39 | music_manager = new AudioManagerMusic(); 40 | 41 | 42 | if (not music_manager) 43 | { 44 | throw Exception("Out of memory for a new AudioManagerMusic"); 45 | } 46 | } 47 | 48 | AudioManagerSfx * 49 | AudioManagerWrapper::sfx() const 50 | { 51 | return sfx_manager; 52 | } 53 | 54 | AudioManagerMusic * 55 | AudioManagerWrapper::music() const 56 | { 57 | return music_manager; 58 | } 59 | 60 | void 61 | AudioManagerWrapper::open_audio() 62 | { 63 | SDL_InitSubSystem(SDL_INIT_AUDIO); 64 | Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096); 65 | Mix_AllocateChannels(4000); 66 | } 67 | 68 | void 69 | AudioManagerWrapper::close_audio() 70 | { 71 | // Query the specifications of the opened audio, to figure out how many times we need to 72 | // close the audio. 73 | int frequency = 0; 74 | int channels = 0; 75 | Uint16 format = 0; 76 | const int k_numbers_of_times_opened = Mix_QuerySpec(&frequency, &format, &channels); 77 | int times_to_close = k_numbers_of_times_opened; 78 | 79 | if (k_numbers_of_times_opened == 0) 80 | { 81 | cout << "Error querying the specifications of the audio. " << Mix_GetError() << endl; 82 | 83 | // Set the amount of times to close the audio to one, just for safety. 84 | times_to_close = 1; 85 | } 86 | 87 | // Close the audio the necessary amount of times. 88 | while (times_to_close > 0) 89 | { 90 | Mix_CloseAudio(); 91 | times_to_close--; 92 | } 93 | 94 | // "Since each call to Mix_Init may set different flags, there is no way, currently, to 95 | // request how many times each one was initted. In other words, the only way to quit for 96 | // sure is to do a loop like so: " 97 | while (Mix_Init(0)) 98 | { 99 | Mix_Quit(); 100 | } 101 | 102 | SDL_QuitSubSystem(SDL_INIT_AUDIO); 103 | } 104 | -------------------------------------------------------------------------------- /engine/src/bitmap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um mapa de bits. 3 | * 4 | * Autor: Rodrigo Gonçalves 5 | * Data: 13/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/bitmap.h" 9 | #include "core/rect.h" 10 | #include "core/exception.h" 11 | #include "core/environment.h" 12 | 13 | #include 14 | #include 15 | 16 | class Bitmap::Impl 17 | { 18 | public: 19 | Impl(void *data, int w, int h) 20 | : m_w(w), m_h(h) 21 | { 22 | m_bitmap = static_cast(data); 23 | } 24 | 25 | ~Impl() 26 | { 27 | if (m_bitmap) 28 | { 29 | SDL_FreeSurface(m_bitmap); 30 | } 31 | } 32 | 33 | int w() const { return m_w; } 34 | int h() const { return m_h; } 35 | SDL_Surface * data() const { return m_bitmap; } 36 | void * pixels() const { return m_bitmap->pixels; } 37 | void clear() { SDL_FillRect(m_bitmap, nullptr, 0); } 38 | 39 | void fill(const Rect& r, Uint32 color) 40 | { 41 | SDL_Rect rect { (int) r.x(), (int) r.y(), (int) r.w(), (int) r.h() }; 42 | 43 | SDL_FillRect(m_bitmap, &rect, color); 44 | } 45 | 46 | private: 47 | int m_w, m_h; 48 | SDL_Surface *m_bitmap; 49 | }; 50 | 51 | Bitmap::Bitmap(void *data, int w, int h) 52 | : m_impl(new Impl(data, w, h)) 53 | { 54 | } 55 | 56 | Bitmap::Bitmap(Canvas *canvas) 57 | : m_impl(new Impl((void *) SDL_CreateRGBSurface(0, canvas->w(), canvas->h(), 32, 0, 0, 0, 0), 58 | canvas->w(), canvas->h())) 59 | { 60 | } 61 | 62 | Bitmap::~Bitmap() 63 | { 64 | } 65 | 66 | void * 67 | Bitmap::data() const 68 | { 69 | return m_impl->data(); 70 | } 71 | 72 | int 73 | Bitmap::w() const 74 | { 75 | return m_impl->w(); 76 | } 77 | 78 | int 79 | Bitmap::h() const 80 | { 81 | return m_impl->h(); 82 | } 83 | 84 | Bitmap * 85 | Bitmap::from_file(const string& path) throw (Exception) 86 | { 87 | SDL_Surface *bitmap = IMG_Load(path.c_str()); 88 | 89 | if (bitmap == nullptr) 90 | { 91 | throw Exception(SDL_GetError()); 92 | } 93 | 94 | Bitmap *b = new Bitmap(bitmap, bitmap->w, bitmap->h); 95 | 96 | if (not b) 97 | { 98 | throw Exception("Out of memory for a new Bitmap"); 99 | } 100 | 101 | return b; 102 | } 103 | 104 | void * 105 | Bitmap::pixels() const 106 | { 107 | return m_impl->pixels(); 108 | } 109 | 110 | void 111 | Bitmap::clear() 112 | { 113 | m_impl->clear(); 114 | } 115 | 116 | Uint32 117 | Bitmap::getpixel(SDL_Surface *surface, int x, int y) 118 | { 119 | int bpp = surface->format->BytesPerPixel; 120 | Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; 121 | 122 | switch (bpp) 123 | { 124 | case 1: 125 | return *p; 126 | break; 127 | 128 | case 2: 129 | return *(Uint16 *)p; 130 | break; 131 | 132 | case 3: 133 | if (SDL_BYTEORDER == SDL_BIG_ENDIAN) 134 | { 135 | return p[0] << 16 | p[1] << 8 | p[2]; 136 | } 137 | else 138 | { 139 | return p[0] | p[1] << 8 | p[2] << 16; 140 | } 141 | break; 142 | 143 | case 4: 144 | return *(Uint32 *)p; 145 | break; 146 | 147 | default: 148 | return 0; 149 | } 150 | } 151 | 152 | void 153 | Bitmap::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) 154 | { 155 | int bpp = surface->format->BytesPerPixel; 156 | Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; 157 | 158 | switch (bpp) 159 | { 160 | case 1: 161 | *p = pixel; 162 | break; 163 | 164 | case 2: 165 | *(Uint16 *)p = pixel; 166 | break; 167 | 168 | case 3: 169 | if (SDL_BYTEORDER == SDL_BIG_ENDIAN) 170 | { 171 | p[0] = (pixel >> 16) & 0xff; 172 | p[1] = (pixel >> 8) & 0xff; 173 | p[2] = pixel & 0xff; 174 | } 175 | else 176 | { 177 | p[0] = pixel & 0xff; 178 | p[1] = (pixel >> 8) & 0xff; 179 | p[2] = (pixel >> 16) & 0xff; 180 | } 181 | break; 182 | 183 | case 4: 184 | *(Uint32 *)p = pixel; 185 | break; 186 | } 187 | } 188 | 189 | void 190 | Bitmap::fill(const Rect& r, Uint32 color) 191 | { 192 | m_impl->fill(r, color); 193 | } 194 | -------------------------------------------------------------------------------- /engine/src/button.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Button. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 29/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/color.h" 9 | #include "core/rect.h" 10 | #include "core/text.h" 11 | #include "core/texture.h" 12 | #include "util/button.h" 13 | #include "core/environment.h" 14 | #include "core/mousebuttonevent.h" 15 | #include "core/mousemotionevent.h" 16 | 17 | #include 18 | 19 | class Button::Impl 20 | { 21 | public: 22 | typedef enum { IDLE, ACTIVE } State; 23 | 24 | Impl(Button *button) 25 | : m_button(button), m_text(nullptr), m_idle(Color::GREEN), 26 | m_active(Color::BLUE), m_border(Color::BLACK), m_thickness(1), 27 | m_state(IDLE), m_idle_texture(nullptr), m_active_texture(nullptr) 28 | { 29 | } 30 | 31 | Impl(Button *button, const string& idle_image_id, 32 | const string& active_image_id) 33 | : m_button(button), m_text(nullptr), m_idle(Color::GREEN), 34 | m_active(Color::BLUE), m_border(Color::BLACK), m_thickness(1), 35 | m_state(IDLE) 36 | { 37 | Environment *env = Environment::get_instance(); 38 | 39 | m_idle_texture = env->resources_manager->get_texture(idle_image_id); 40 | m_active_texture = env->resources_manager->get_texture(active_image_id); 41 | 42 | m_button->set_position(0.0, 0.0); 43 | m_button->set_dimensions(m_idle_texture->w(), m_idle_texture->h()); 44 | } 45 | 46 | 47 | ~Impl() 48 | { 49 | delete m_text; 50 | } 51 | 52 | void set_text(const string& text, const Color& color) 53 | { 54 | if (text.size() > 0) 55 | { 56 | delete m_text; 57 | m_text = new Text(m_button, text, color); 58 | } 59 | } 60 | 61 | void set_color(const Color& idle, const Color& active) 62 | { 63 | m_idle = idle; 64 | m_active = active; 65 | } 66 | 67 | void set_border(int thickness, const Color& color) 68 | { 69 | m_border = color; 70 | m_thickness = thickness; 71 | } 72 | 73 | bool on_event(const MouseButtonEvent& event) 74 | { 75 | if (event.state() == MouseButtonEvent::PRESSED and 76 | event.button() == MouseButtonEvent::LEFT and 77 | m_button->bounding_box().contains(event.x(), event.y())) 78 | { 79 | char coords[64]; 80 | sprintf(coords, "%.2f,%.2f", event.x(), event.y()); 81 | m_button->notify(clickedID, coords); 82 | 83 | return true; 84 | } 85 | 86 | return false; 87 | } 88 | 89 | bool on_event(const MouseMotionEvent& event) 90 | { 91 | if (m_button->bounding_box().contains(event.x(), event.y())) 92 | { 93 | m_state = ACTIVE; 94 | 95 | if (m_active_texture.get()) 96 | { 97 | m_button->set_dimensions(m_active_texture->w(), m_active_texture->h()); 98 | } 99 | 100 | return true; 101 | } else 102 | { 103 | m_state = IDLE; 104 | 105 | if (m_idle_texture.get()) 106 | { 107 | m_button->set_dimensions(m_idle_texture->w(), m_idle_texture->h()); 108 | } 109 | } 110 | 111 | return false; 112 | } 113 | 114 | void draw_self() 115 | { 116 | Environment *env = Environment::get_instance(); 117 | shared_ptr image; 118 | Color color; 119 | 120 | switch (m_state) 121 | { 122 | case IDLE: 123 | image = m_idle_texture; 124 | color = m_idle; 125 | break; 126 | 127 | case ACTIVE: 128 | image = m_active_texture; 129 | color = m_active; 130 | break; 131 | } 132 | 133 | if (image.get()) 134 | { 135 | env->canvas->draw(image.get(), m_button->x(), m_button->y()); 136 | } 137 | else 138 | { 139 | env->canvas->set_blend_mode(Canvas::BLEND); 140 | 141 | Rect r = m_button->bounding_box(); 142 | 143 | for (int i = 0; i < m_thickness; ++i) 144 | { 145 | env->canvas->draw(r, m_border); 146 | 147 | r.set_position(r.x() + 1, r.y() + 1); 148 | r.set_dimensions(r.w() - 2, r.h() - 2); 149 | } 150 | 151 | env->canvas->fill(r, color); 152 | env->canvas->set_blend_mode(Canvas::NONE); 153 | } 154 | 155 | if (m_text) 156 | { 157 | m_text->align_to(m_button, Object::CENTER, Object::MIDDLE); 158 | m_text->draw(); 159 | } 160 | } 161 | 162 | private: 163 | Button *m_button; 164 | Text *m_text; 165 | Color m_idle, m_active, m_border; 166 | int m_thickness; 167 | State m_state; 168 | shared_ptr m_idle_texture, m_active_texture; 169 | }; 170 | 171 | ActionID Button::clickedID = "clicked()"; 172 | 173 | Button::Button(Object *parent, ObjectID id, double w, double h) 174 | : Object(parent, id, 0, 0, w, h), m_impl(new Impl(this)) 175 | { 176 | Environment *env = Environment::get_instance(); 177 | env->events_manager->register_listener(this); 178 | } 179 | 180 | Button::Button(Object *parent, ObjectID id, const string& idle_image_id, 181 | const string& active_image_id) 182 | : Object(parent, id), m_impl(new Impl(this, idle_image_id, active_image_id)) 183 | { 184 | Environment *env = Environment::get_instance(); 185 | env->events_manager->register_listener(this); 186 | } 187 | 188 | Button::~Button() 189 | { 190 | Environment *env = Environment::get_instance(); 191 | env->events_manager->unregister_listener(this); 192 | } 193 | 194 | void 195 | Button::draw_self() 196 | { 197 | m_impl->draw_self(); 198 | } 199 | 200 | bool 201 | Button::on_event(const MouseButtonEvent& event) 202 | { 203 | return m_impl->on_event(event); 204 | } 205 | 206 | bool 207 | Button::on_event(const MouseMotionEvent& event) 208 | { 209 | return m_impl->on_event(event); 210 | } 211 | 212 | void 213 | Button::set_text(const string& text, const Color& color) 214 | { 215 | return m_impl->set_text(text, color); 216 | } 217 | 218 | void 219 | Button::set_color(const Color& idle, const Color& active) 220 | { 221 | return m_impl->set_color(idle, active); 222 | } 223 | 224 | void 225 | Button::set_border(int thickness, const Color& color) 226 | { 227 | return m_impl->set_border(thickness, color); 228 | } 229 | -------------------------------------------------------------------------------- /engine/src/camera.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Camera. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 27/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/camera.h" 9 | #include "core/rect.h" 10 | 11 | #include 12 | using namespace std; 13 | 14 | class Camera::Impl 15 | { 16 | public: 17 | Impl(Camera *camera, Mode mode) 18 | : m_camera(camera), m_mode(mode), m_target(nullptr), 19 | m_limits(-1000000, -1000000, 2000000, 2000000) {} 20 | 21 | Mode mode() const { return m_mode; } 22 | void set_mode(Mode mode) { m_mode = mode; } 23 | 24 | void follow(const Object *target) { m_target = target; } 25 | 26 | void update_self(unsigned long) 27 | { 28 | if (m_mode == Camera::FOLLOWING and m_target) 29 | { 30 | double x = m_target->x() + (m_target->w() - m_camera->w())/2; 31 | double y = m_target->y() + (m_target->h() - m_camera->h())/2; 32 | 33 | if (x < m_limits.x()) 34 | { 35 | x = m_limits.x(); 36 | } 37 | 38 | if (x + m_camera->w() > m_limits.x() + m_limits.w()) 39 | { 40 | x = m_limits.x() + m_limits.w() - m_camera->w(); 41 | } 42 | 43 | if (y < m_limits.y()) 44 | { 45 | y = m_limits.y(); 46 | } 47 | 48 | if (y + m_camera->h() > m_limits.y() + m_limits.h()) 49 | { 50 | y = m_limits.y() + m_limits.h() - m_camera->h(); 51 | } 52 | 53 | m_camera->set_position(x, y); 54 | } 55 | } 56 | 57 | void draw_self() {} 58 | 59 | void set_limits(const Rect& limits) 60 | { 61 | m_limits = limits; 62 | } 63 | 64 | private: 65 | Camera *m_camera; 66 | Mode m_mode; 67 | const Object *m_target; 68 | Rect m_limits; 69 | }; 70 | 71 | Camera::Camera(double x, double y, double w, double h, Mode mode) 72 | : Object(nullptr, "", x, y, w, h), m_impl(new Camera::Impl(this, mode)) 73 | { 74 | } 75 | 76 | Camera::~Camera() 77 | { 78 | } 79 | 80 | Camera::Mode 81 | Camera::mode() const 82 | { 83 | return m_impl->mode(); 84 | } 85 | 86 | void 87 | Camera::set_mode(Mode mode) 88 | { 89 | m_impl->set_mode(mode); 90 | } 91 | 92 | void 93 | Camera::update_self(unsigned long elapsed) 94 | { 95 | m_impl->update_self(elapsed); 96 | } 97 | 98 | void 99 | Camera::draw_self() 100 | { 101 | m_impl->draw_self(); 102 | } 103 | 104 | void 105 | Camera::follow(const Object* object) 106 | { 107 | m_impl->follow(object); 108 | } 109 | 110 | void 111 | Camera::set_limits(const Rect& limits) 112 | { 113 | m_impl->set_limits(limits); 114 | } 115 | -------------------------------------------------------------------------------- /engine/src/circle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um círculo no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/circle.h" 9 | 10 | Circle::Circle(const Point& center, double radius) 11 | : m_center(center), m_r(radius) 12 | { 13 | } 14 | 15 | Point 16 | Circle::center() const 17 | { 18 | return m_center; 19 | } 20 | 21 | double 22 | Circle::radius() const 23 | { 24 | return m_r; 25 | } 26 | 27 | void 28 | Circle::set_center(const Point& center) 29 | { 30 | m_center = center; 31 | } 32 | 33 | void 34 | Circle::set_radius(double radius) 35 | { 36 | m_r = radius; 37 | } 38 | 39 | void 40 | Circle::set(const Point& center, double radius) 41 | { 42 | m_center = center; 43 | m_r = radius; 44 | } 45 | -------------------------------------------------------------------------------- /engine/src/color.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa uma cor. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/color.h" 9 | 10 | Color::Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) 11 | : m_r(r), m_g(g), m_b(b), m_a(a) 12 | { 13 | } 14 | 15 | bool 16 | Color::operator!=(const Color& color) const 17 | { 18 | return m_r != color.m_r or m_b != color.m_b or m_g != color.m_g or m_a != color.m_a; 19 | } 20 | 21 | unsigned char 22 | Color::r() const 23 | { 24 | return m_r; 25 | } 26 | 27 | unsigned char 28 | Color::g() const 29 | { 30 | return m_g; 31 | } 32 | 33 | unsigned char 34 | Color::b() const 35 | { 36 | return m_b; 37 | } 38 | 39 | unsigned char 40 | Color::a() const 41 | { 42 | return m_a; 43 | } 44 | 45 | void 46 | Color::set_r(unsigned char r) 47 | { 48 | m_r = r; 49 | } 50 | 51 | void 52 | Color::set_g(unsigned char g) 53 | { 54 | m_g = g; 55 | } 56 | 57 | void 58 | Color::set_b(unsigned char b) 59 | { 60 | m_b = b; 61 | } 62 | 63 | void 64 | Color::set_a(unsigned char a) 65 | { 66 | m_a = a; 67 | } 68 | 69 | void 70 | Color::set(unsigned char r, unsigned char g, unsigned char b, unsigned char a) 71 | { 72 | m_r = r; 73 | m_b = b; 74 | m_g = g; 75 | m_a = a; 76 | } 77 | 78 | Color Color::WHITE {255, 255, 255}; 79 | Color Color::BLACK {0, 0, 0}; 80 | Color Color::RED {255, 0, 0}; 81 | Color Color::GREEN {0, 255, 0}; 82 | Color Color::BLUE {0, 0, 255}; 83 | Color Color::YELLOW {255, 255, 0}; 84 | Color Color::CYAN {0, 255, 255}; 85 | Color Color::MAGENTA {255, 0, 255}; 86 | Color Color::TRANSPARENT {255, 255, 255, 0}; 87 | -------------------------------------------------------------------------------- /engine/src/environment.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Environment. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 06/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/environment.h" 9 | #include "core/video.h" 10 | #include "core/camera.h" 11 | 12 | static Environment *env = nullptr; 13 | 14 | Environment::Environment() 15 | : video(nullptr), resources_manager(nullptr), events_manager(nullptr), 16 | audio_manager(nullptr), canvas(nullptr), camera(nullptr), music(nullptr), 17 | sfx(nullptr), m_settings_path("") 18 | { 19 | } 20 | 21 | Environment::~Environment() 22 | { 23 | delete audio_manager; 24 | delete events_manager; 25 | delete resources_manager; 26 | delete video; 27 | } 28 | 29 | Environment * 30 | Environment::get_instance() throw (Exception) 31 | { 32 | if (not env) 33 | { 34 | env = new Environment(); 35 | 36 | if (not env) 37 | { 38 | throw Exception("Out of memory for a new Environment"); 39 | } 40 | 41 | env->init(); 42 | } 43 | 44 | return env; 45 | } 46 | 47 | void 48 | Environment::release_instance() 49 | { 50 | delete env; 51 | env = nullptr; 52 | } 53 | 54 | void 55 | Environment::init() throw (Exception) 56 | { 57 | video = new Video(); 58 | 59 | if (not video) 60 | { 61 | throw Exception("Out of memory for a new Video"); 62 | } 63 | 64 | video->init(); 65 | canvas = video->canvas(); 66 | camera = video->camera(); 67 | 68 | resources_manager = new ResourcesManager(); 69 | 70 | if (not resources_manager) 71 | { 72 | throw Exception("Out of memory for a new ResourcesManager"); 73 | } 74 | 75 | events_manager = new EventsManager(); 76 | 77 | if (not events_manager) 78 | { 79 | throw Exception("Out of memory for a new EventsManager"); 80 | } 81 | 82 | audio_manager = new AudioManagerWrapper(); 83 | 84 | if (not audio_manager) 85 | { 86 | throw Exception("Out of memory for a new AudioManagerSfx"); 87 | } 88 | 89 | audio_manager->init(); 90 | 91 | music = audio_manager->music(); 92 | sfx = audio_manager->sfx(); 93 | } 94 | -------------------------------------------------------------------------------- /engine/src/eventsmanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que gerencia os eventos de input do jogo. 3 | * 4 | * Autor: Carlos Oliveira 5 | * Data: 17/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/eventsmanager.h" 9 | #include "core/systemevent.h" 10 | #include "core/keyboardevent.h" 11 | #include "core/mousebuttonevent.h" 12 | #include "core/mousemotionevent.h" 13 | #include "core/joystickevent.h" 14 | #include "core/listener.h" 15 | 16 | #include 17 | using std::list; 18 | 19 | list 20 | grab_SDL_events() 21 | { 22 | SDL_Event event; 23 | list events; 24 | 25 | while (SDL_PollEvent(&event)) 26 | { 27 | events.push_back(event); 28 | } 29 | 30 | return events; 31 | } 32 | 33 | class EventsManager::Impl 34 | { 35 | public: 36 | Impl() {} 37 | ~Impl() {} 38 | 39 | void register_listener(Listener *listener) 40 | { 41 | if (listener) 42 | { 43 | m_listeners.push_back(listener); 44 | } 45 | } 46 | 47 | void unregister_listener(Listener *listener) 48 | { 49 | m_listeners.remove(listener); 50 | } 51 | 52 | void dispatch_pending_events() 53 | { 54 | 55 | list events = grab_SDL_events(); 56 | 57 | for (auto e : events) 58 | { 59 | switch (e.type) 60 | { 61 | case SDL_QUIT: 62 | { 63 | SystemEvent se = SystemEvent::from_SDL(e); 64 | 65 | for (auto ls : m_listeners) 66 | { 67 | if (ls->active() and ls->on_event(se)) 68 | { 69 | break; 70 | } 71 | } 72 | 73 | break; 74 | } 75 | 76 | case SDL_MOUSEMOTION: 77 | { 78 | MouseMotionEvent me = MouseMotionEvent::from_SDL(e); 79 | 80 | for (auto ls : m_listeners) 81 | { 82 | if (ls->active() and ls->on_event(me)) 83 | { 84 | break; 85 | } 86 | } 87 | 88 | break; 89 | } 90 | 91 | 92 | case SDL_MOUSEBUTTONDOWN: 93 | case SDL_MOUSEBUTTONUP: 94 | { 95 | MouseButtonEvent me = MouseButtonEvent::from_SDL(e); 96 | 97 | for (auto ls : m_listeners) 98 | { 99 | if (ls->active() and ls->on_event(me)) 100 | { 101 | break; 102 | } 103 | } 104 | 105 | break; 106 | } 107 | 108 | case SDL_KEYDOWN: 109 | case SDL_KEYUP: 110 | { 111 | if (e.key.repeat != 0) 112 | { 113 | break; 114 | } 115 | 116 | KeyboardEvent ke = KeyboardEvent::from_SDL(e); 117 | 118 | for (auto ls : m_listeners) 119 | { 120 | if (ls->active() and ls->on_event(ke)) 121 | { 122 | break; 123 | } 124 | } 125 | 126 | break; 127 | } 128 | 129 | case SDL_CONTROLLERBUTTONDOWN: 130 | case SDL_CONTROLLERBUTTONUP: 131 | { 132 | JoyStickEvent je = JoyStickEvent::from_SDL(e); 133 | 134 | for (auto ls : m_listeners) 135 | { 136 | if (ls->active() and ls->on_event(je)) 137 | { 138 | break; 139 | } 140 | } 141 | 142 | break; 143 | } 144 | } 145 | } 146 | } 147 | 148 | private: 149 | list m_listeners; 150 | }; 151 | 152 | EventsManager::EventsManager() 153 | : m_impl(new Impl()) 154 | { 155 | } 156 | 157 | EventsManager::~EventsManager() 158 | { 159 | } 160 | 161 | void 162 | EventsManager::dispatch_pending_events() 163 | { 164 | m_impl->dispatch_pending_events(); 165 | } 166 | 167 | void 168 | EventsManager::register_listener(Listener *listener) 169 | { 170 | m_impl->register_listener(listener); 171 | } 172 | 173 | void 174 | EventsManager::unregister_listener(Listener *listener) 175 | { 176 | m_impl->unregister_listener(listener); 177 | } 178 | -------------------------------------------------------------------------------- /engine/src/font.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa uma fonte. 3 | * 4 | * Autor: Carlos Oliveira 5 | * Data: 18/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/font.h" 9 | #include "core/environment.h" 10 | #include "core/settings.h" 11 | 12 | class Font::Impl 13 | { 14 | public: 15 | Impl(TTF_Font *font, const string& path, int size) 16 | : m_font(font), m_path(path), m_size(size), m_style(NORMAL) 17 | { 18 | } 19 | 20 | ~Impl() 21 | { 22 | if (m_font) 23 | { 24 | TTF_CloseFont(m_font); 25 | } 26 | } 27 | 28 | TTF_Font * font() const 29 | { 30 | return m_font; 31 | } 32 | 33 | int size() const 34 | { 35 | return m_size; 36 | } 37 | 38 | Style style() const 39 | { 40 | return m_style; 41 | } 42 | 43 | void set_size(int size) 44 | { 45 | Environment *env = Environment::get_instance(); 46 | shared_ptr settings = env->resources_manager->get_settings(env->m_settings_path); 47 | double k = settings->read("Game", "scale", 1); 48 | 49 | change_size(size * k); 50 | } 51 | 52 | void set_style(Style style) 53 | { 54 | switch (style) 55 | { 56 | case BOLD: 57 | TTF_SetFontStyle(m_font, TTF_STYLE_BOLD); 58 | break; 59 | 60 | case ITALIC: 61 | TTF_SetFontStyle(m_font, TTF_STYLE_ITALIC); 62 | break; 63 | 64 | default: 65 | TTF_SetFontStyle(m_font, TTF_STYLE_NORMAL); 66 | break; 67 | } 68 | } 69 | 70 | private: 71 | TTF_Font *m_font; 72 | string m_path; 73 | int m_size; 74 | Style m_style; 75 | 76 | void change_size(int size) 77 | { 78 | if (m_size == size) 79 | { 80 | return; 81 | } 82 | 83 | TTF_Font *font = TTF_OpenFont(m_path.c_str(), size); 84 | 85 | if (not font) 86 | { 87 | return; 88 | } 89 | 90 | TTF_CloseFont(m_font); 91 | m_font = font; 92 | m_size = size; 93 | } 94 | }; 95 | 96 | Font::Font(TTF_Font *font, const string& path, int size) 97 | : m_impl(new Font::Impl(font, path, size)) 98 | { 99 | } 100 | 101 | Font::~Font() 102 | { 103 | } 104 | 105 | Font * 106 | Font::from_file(const string& path) throw (Exception) 107 | { 108 | TTF_Font *font = TTF_OpenFont(path.c_str(), 20); 109 | 110 | if (not font) 111 | { 112 | throw Exception(TTF_GetError()); 113 | } 114 | 115 | return new Font(font, path); 116 | } 117 | 118 | TTF_Font * 119 | Font::font() const 120 | { 121 | return m_impl->font(); 122 | } 123 | 124 | int 125 | Font::size() const 126 | { 127 | return m_impl->size(); 128 | } 129 | 130 | Font::Style 131 | Font::style() const 132 | { 133 | return m_impl->style(); 134 | } 135 | 136 | void 137 | Font::set_size(int size) 138 | { 139 | m_impl->set_size(size); 140 | } 141 | 142 | void 143 | Font::set_style(Style style) 144 | { 145 | m_impl->set_style(style); 146 | } 147 | -------------------------------------------------------------------------------- /engine/src/frontend.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da class FrontEnd. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "util/frontend.h" 9 | 10 | #include "core/rect.h" 11 | #include "core/texture.h" 12 | #include "core/environment.h" 13 | 14 | #include "core/joystickevent.h" 15 | #include "core/keyboardevent.h" 16 | #include "core/mousebuttonevent.h" 17 | 18 | class FrontEnd::Impl 19 | { 20 | public: 21 | Impl(Level *parent, const string& texture, unsigned long duration, 22 | const Color& background) 23 | : m_parent(parent), m_texture(nullptr), m_background(background), 24 | m_fad(), m_start(0), m_duration(duration) 25 | { 26 | Environment *env = Environment::get_instance(); 27 | m_texture = env->resources_manager->get_texture(texture); 28 | 29 | m_in = m_duration / 3; 30 | m_out = m_duration - m_in; 31 | } 32 | 33 | ~Impl() {} 34 | 35 | bool on_event(const MouseButtonEvent& event) 36 | { 37 | if (event.state() == MouseButtonEvent::PRESSED) 38 | { 39 | m_parent->finish(); 40 | return true; 41 | } 42 | 43 | return false; 44 | } 45 | 46 | bool on_event(const KeyboardEvent& event) 47 | { 48 | if (event.state() == KeyboardEvent::PRESSED) 49 | { 50 | m_parent->finish(); 51 | return true; 52 | } 53 | 54 | return false; 55 | } 56 | 57 | bool on_event(const JoyStickEvent& event) 58 | { 59 | if (event.state() == JoyStickEvent::PRESSED) 60 | { 61 | m_parent->finish(); 62 | return true; 63 | } 64 | 65 | return false; 66 | } 67 | 68 | void update_self(unsigned long elapsed) 69 | { 70 | if (not m_start) 71 | { 72 | m_start = elapsed; 73 | } 74 | 75 | unsigned long now = elapsed - m_start; 76 | 77 | if (now <= m_in) 78 | { 79 | unsigned char a = (255 * (100 - (100*now)/m_in))/100; 80 | m_fad.set_a(a); 81 | } else if (now >= m_out) 82 | { 83 | unsigned char a = (255 * (100*(now - m_out))/m_in)/100; 84 | m_fad.set_a(a); 85 | } else 86 | { 87 | m_fad.set_a(0); 88 | } 89 | 90 | if (now > m_duration) 91 | { 92 | m_parent->finish(); 93 | } 94 | 95 | Environment *env = Environment::get_instance(); 96 | 97 | m_x = (env->camera->w() - m_texture->w())/2 + env->camera->x(); 98 | m_y = (env->camera->h() - m_texture->h())/2 + env->camera->y(); 99 | } 100 | 101 | void draw_self() 102 | { 103 | Environment *env = Environment::get_instance(); 104 | 105 | env->canvas->clear(m_background); 106 | env->canvas->draw(m_texture.get(), m_x, m_y); 107 | 108 | env->canvas->set_blend_mode(Canvas::BLEND); 109 | Rect r { 0, 0, (double) env->canvas->w(), (double) env->canvas->h() }; 110 | env->canvas->fill(r, m_fad); 111 | env->canvas->set_blend_mode(Canvas::NONE); 112 | } 113 | 114 | private: 115 | Level *m_parent; 116 | shared_ptr m_texture; 117 | Color m_background, m_fad; 118 | unsigned long m_start, m_duration; 119 | unsigned long m_in, m_out; 120 | int m_x, m_y; 121 | }; 122 | 123 | FrontEnd::FrontEnd(const string& id, const string& next, const string& texture, 124 | unsigned long duration, const Color& bg) 125 | : Level(id, next), m_impl(new Impl(this, texture, duration, bg)) 126 | { 127 | Environment *env = Environment::get_instance(); 128 | env->events_manager->register_listener(this); 129 | } 130 | 131 | FrontEnd::~FrontEnd() 132 | { 133 | Environment *env = Environment::get_instance(); 134 | env->events_manager->unregister_listener(this); 135 | } 136 | 137 | void 138 | FrontEnd::draw_self() 139 | { 140 | m_impl->draw_self(); 141 | } 142 | 143 | void 144 | FrontEnd::update_self(unsigned long elapsed) 145 | { 146 | m_impl->update_self(elapsed); 147 | } 148 | 149 | bool 150 | FrontEnd::on_event(const MouseButtonEvent& event) 151 | { 152 | return m_impl->on_event(event); 153 | } 154 | 155 | bool 156 | FrontEnd::on_event(const JoyStickEvent& event) 157 | { 158 | return m_impl->on_event(event); 159 | } 160 | 161 | bool 162 | FrontEnd::on_event(const KeyboardEvent& event) 163 | { 164 | return m_impl->on_event(event); 165 | } 166 | -------------------------------------------------------------------------------- /engine/src/game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um jogo utilizando a SDL. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 26/03/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/game.h" 9 | #include "core/video.h" 10 | #include "core/environment.h" 11 | #include "core/systemevent.h" 12 | #include "core/keyboardevent.h" 13 | #include "core/settings.h" 14 | 15 | #include 16 | #include 17 | 18 | Game::Game(const string& id) 19 | : m_id(id), m_level(nullptr), m_done(false) 20 | { 21 | env = Environment::get_instance(); 22 | 23 | } 24 | 25 | Game::~Game() 26 | { 27 | if (m_level) 28 | { 29 | delete m_level; 30 | } 31 | 32 | env->events_manager->unregister_listener(this); 33 | Environment::release_instance(); 34 | } 35 | 36 | void 37 | Game::init(const string& title, int w, int h, double scale, bool fullscreen, 38 | int volume) throw (Exception) 39 | { 40 | env->video->set_resolution(w, h, scale); 41 | env->video->set_window_name(title); 42 | env->video->set_fullscreen(fullscreen); 43 | env->music->set_volume(volume); 44 | env->sfx->set_volume(volume); 45 | 46 | SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); 47 | 48 | if (SDL_NumJoysticks() > 0) 49 | { 50 | SDL_GameControllerOpen(0); 51 | } 52 | 53 | env->events_manager->register_listener(this); 54 | 55 | m_level = load_level(m_id); 56 | } 57 | 58 | void 59 | Game::init(const string& path) throw (Exception) 60 | { 61 | env->m_settings_path = path; 62 | 63 | shared_ptr settings = env->resources_manager->get_settings(path); 64 | 65 | string title = settings->read("Game", "title", "Test Game"); 66 | int w = settings->read("Game", "w", 800); 67 | int h = settings->read("Game", "h", 600); 68 | double scale = settings->read("Game", "scale", 1); 69 | bool fullscreen = settings->read("Game", "fullscreen", false); 70 | int volume = settings->read("Game", "volume", 50); 71 | 72 | init(title, w, h, scale, fullscreen, volume); 73 | } 74 | 75 | void 76 | Game::run() 77 | { 78 | while (m_level and not m_done) 79 | { 80 | unsigned long now = update_timestep(); 81 | env->events_manager->dispatch_pending_events(); 82 | 83 | m_level->update(now); 84 | env->camera->update(now); 85 | 86 | m_level->draw(); 87 | update_screen(); 88 | delay(1); 89 | 90 | if (m_level->finished()) 91 | { 92 | string next = m_level->next(); 93 | delete m_level; 94 | m_level = load_level(next); 95 | } 96 | } 97 | } 98 | 99 | unsigned long 100 | Game::update_timestep() const 101 | { 102 | return SDL_GetTicks(); 103 | } 104 | 105 | bool 106 | Game::on_event(const SystemEvent& event) 107 | { 108 | if (event.type() == SystemEvent::QUIT) 109 | { 110 | m_done = true; 111 | return true; 112 | } 113 | 114 | return false; 115 | } 116 | 117 | bool 118 | Game::on_event(const KeyboardEvent& event) 119 | { 120 | if (event.state() == KeyboardEvent::PRESSED 121 | and event.key() == KeyboardEvent::ESCAPE) 122 | { 123 | m_done = true; 124 | return true; 125 | } 126 | 127 | return false; 128 | } 129 | 130 | void 131 | Game::update_screen() 132 | { 133 | Environment *env = Environment::get_instance(); 134 | env->canvas->update(); 135 | } 136 | 137 | void 138 | Game::delay(unsigned long ms) 139 | { 140 | SDL_Delay(ms); 141 | } 142 | 143 | Level * 144 | Game::load_level(const string&) 145 | { 146 | return nullptr; 147 | } 148 | -------------------------------------------------------------------------------- /engine/src/image.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa uma imagem. 3 | * 4 | * Autor: Jefferson Xavier 5 | * Data: 16/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/image.h" 9 | 10 | #include "core/texture.h" 11 | #include "core/environment.h" 12 | 13 | class Image::Impl 14 | { 15 | public: 16 | Impl(Image *parent, const string& texture) 17 | : m_parent(parent) 18 | { 19 | Environment *env = Environment::get_instance(); 20 | m_texture = env->resources_manager->get_texture(texture); 21 | parent->set_dimensions(m_texture->w(), m_texture->h()); 22 | } 23 | 24 | void draw_self() 25 | { 26 | Environment *env = Environment::get_instance(); 27 | env->canvas->draw(m_texture.get(), m_parent->x(), m_parent->y()); 28 | } 29 | 30 | private: 31 | Image *m_parent; 32 | shared_ptr m_texture; 33 | }; 34 | 35 | Image::Image(Object *parent, const string& texture) 36 | : Object(parent, texture), m_impl(new Impl(this, texture)) 37 | { 38 | } 39 | 40 | Image::~Image() 41 | { 42 | } 43 | 44 | void 45 | Image::draw_self() 46 | { 47 | m_impl->draw_self(); 48 | } 49 | -------------------------------------------------------------------------------- /engine/src/joystickevent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um evento de controle. 3 | * 4 | * Autor: Simiao Carvalho 5 | * Data: 26/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/joystickevent.h" 9 | 10 | #include 11 | #include 12 | 13 | using std::map; 14 | 15 | static bool joystick_was_init = false; 16 | static map m_joystick_table; 17 | 18 | void init_table_joystick() 19 | { 20 | m_joystick_table[SDL_CONTROLLER_BUTTON_DPAD_UP] = JoyStickEvent::UP; 21 | m_joystick_table[SDL_CONTROLLER_BUTTON_DPAD_DOWN] = JoyStickEvent::DOWN; 22 | m_joystick_table[SDL_CONTROLLER_BUTTON_DPAD_LEFT] = JoyStickEvent::LEFT; 23 | m_joystick_table[SDL_CONTROLLER_BUTTON_DPAD_RIGHT ] = JoyStickEvent::RIGHT; 24 | m_joystick_table[SDL_CONTROLLER_BUTTON_A ] = JoyStickEvent::X; 25 | m_joystick_table[SDL_CONTROLLER_BUTTON_B ] = JoyStickEvent::CIRCLE; 26 | m_joystick_table[SDL_CONTROLLER_BUTTON_Y ] = JoyStickEvent::TRIANGLE; 27 | m_joystick_table[SDL_CONTROLLER_BUTTON_X ] = JoyStickEvent::SQUARE; 28 | m_joystick_table[SDL_CONTROLLER_BUTTON_START ] = JoyStickEvent::START; 29 | m_joystick_table[SDL_CONTROLLER_BUTTON_GUIDE ] = JoyStickEvent::HOME; 30 | m_joystick_table[SDL_CONTROLLER_BUTTON_BACK ] = JoyStickEvent::SHARE; 31 | m_joystick_table[SDL_CONTROLLER_BUTTON_LEFTSHOULDER ] = JoyStickEvent::L1; 32 | m_joystick_table[SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ] = JoyStickEvent::R1; 33 | m_joystick_table[SDL_CONTROLLER_BUTTON_LEFTSTICK ] = JoyStickEvent::L3; 34 | m_joystick_table[SDL_CONTROLLER_BUTTON_RIGHTSTICK ] = JoyStickEvent::R3; 35 | } 36 | 37 | JoyStickEvent::JoyStickEvent(State state, Button button) 38 | : m_state(state), m_button(button) 39 | { 40 | } 41 | 42 | JoyStickEvent::State 43 | JoyStickEvent::state() const 44 | { 45 | return m_state; 46 | } 47 | 48 | JoyStickEvent::Button 49 | JoyStickEvent::button() const 50 | { 51 | return m_button; 52 | } 53 | 54 | JoyStickEvent 55 | JoyStickEvent::from_SDL(const SDL_Event& event) 56 | { 57 | if (not joystick_was_init) 58 | { 59 | init_table_joystick(); 60 | joystick_was_init = true; 61 | } 62 | 63 | JoyStickEvent::State state = (event.type == SDL_CONTROLLERBUTTONDOWN ? 64 | JoyStickEvent::PRESSED : JoyStickEvent::RELEASED); 65 | 66 | JoyStickEvent::Button button = m_joystick_table[event.cbutton.button]; 67 | 68 | return JoyStickEvent(state, button); 69 | } 70 | -------------------------------------------------------------------------------- /engine/src/level.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um nível no mundo do jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/level.h" 9 | 10 | Level::Level(const string& id, const string& next) 11 | : Object(nullptr, id), m_next(next), m_done(false) 12 | { 13 | } 14 | 15 | string 16 | Level::next() const 17 | { 18 | return m_next; 19 | } 20 | 21 | bool 22 | Level::finished() const 23 | { 24 | return m_done; 25 | } 26 | 27 | void 28 | Level::finish() 29 | { 30 | m_done = true; 31 | } 32 | 33 | void 34 | Level::set_next(const string& next) 35 | { 36 | m_next = next; 37 | } 38 | -------------------------------------------------------------------------------- /engine/src/line.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa uma reta no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/line.h" 9 | 10 | Line::Line(const Point& a, const Point& b) 11 | : m_a(a), m_b(b) 12 | { 13 | } 14 | 15 | const Point& 16 | Line::a() const 17 | { 18 | return m_a; 19 | } 20 | 21 | const Point& 22 | Line::b() const 23 | { 24 | return m_b; 25 | } 26 | 27 | void 28 | Line::set_a(const Point& a) 29 | { 30 | m_a = a; 31 | } 32 | 33 | void 34 | Line::set_b(const Point& b) 35 | { 36 | m_b = b; 37 | } 38 | 39 | void 40 | Line::set(const Point& a, const Point& b) 41 | { 42 | m_a = a; 43 | m_b = b; 44 | } 45 | -------------------------------------------------------------------------------- /engine/src/listener.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Listener. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 25/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/listener.h" 9 | 10 | class Listener::Impl 11 | { 12 | public: 13 | Impl() : m_active(true) {} 14 | ~Impl() {} 15 | 16 | void set_active(bool active) { m_active = active; } 17 | bool active() const { return m_active; } 18 | 19 | private: 20 | bool m_active; 21 | }; 22 | 23 | Listener::Listener() 24 | : m_impl(new Impl()) 25 | { 26 | } 27 | 28 | Listener::~Listener() 29 | { 30 | } 31 | 32 | bool 33 | Listener::on_event(const SystemEvent&) 34 | { 35 | return false; 36 | } 37 | 38 | bool 39 | Listener::on_event(const KeyboardEvent&) 40 | { 41 | return false; 42 | } 43 | 44 | bool 45 | Listener::on_event(const JoyStickEvent&) 46 | { 47 | return false; 48 | } 49 | 50 | bool 51 | Listener::on_event(const MouseButtonEvent&) 52 | { 53 | return false; 54 | } 55 | 56 | bool 57 | Listener::on_event(const MouseMotionEvent&) 58 | { 59 | return false; 60 | } 61 | 62 | bool 63 | Listener::active() const 64 | { 65 | return m_impl->active(); 66 | } 67 | 68 | void 69 | Listener::set_active(bool active) 70 | { 71 | m_impl->set_active(active); 72 | } 73 | -------------------------------------------------------------------------------- /engine/src/mousebuttonevent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um evento de botão do mouse. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 22/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/mousebuttonevent.h" 9 | 10 | MouseButtonEvent::MouseButtonEvent(ButtonState state, Button button, 11 | double x, double y) : m_state(state), m_button(button), m_x(x), m_y(y) 12 | { 13 | } 14 | 15 | MouseButtonEvent::ButtonState 16 | MouseButtonEvent::state() const 17 | { 18 | return m_state; 19 | } 20 | 21 | MouseButtonEvent::Button 22 | MouseButtonEvent::button() const 23 | { 24 | return m_button; 25 | } 26 | 27 | double 28 | MouseButtonEvent::x() const 29 | { 30 | return m_x; 31 | } 32 | 33 | double 34 | MouseButtonEvent::y() const 35 | { 36 | return m_y; 37 | } 38 | 39 | MouseButtonEvent 40 | MouseButtonEvent::from_SDL(const SDL_Event& event) 41 | { 42 | MouseButtonEvent::ButtonState state = (event.type == SDL_MOUSEBUTTONDOWN ? 43 | MouseButtonEvent::PRESSED : MouseButtonEvent::RELEASED); 44 | 45 | MouseButtonEvent::Button button; 46 | 47 | switch (event.button.button) 48 | { 49 | case SDL_BUTTON_LEFT: 50 | button = MouseButtonEvent::LEFT; 51 | break; 52 | 53 | case SDL_BUTTON_RIGHT: 54 | button = MouseButtonEvent::RIGHT; 55 | break; 56 | 57 | case SDL_BUTTON_MIDDLE: 58 | button = MouseButtonEvent::MIDDLE; 59 | break; 60 | } 61 | 62 | return MouseButtonEvent(state, button, event.button.x, event.button.y); 63 | } 64 | -------------------------------------------------------------------------------- /engine/src/mousemotionevent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um evento de movimentação do mouse. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 25/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/mousemotionevent.h" 9 | 10 | MouseMotionEvent::MouseMotionEvent(int x, int y, int x_rel, int y_rel, 11 | MotionState left, MotionState right, MotionState middle) 12 | : m_x(x), m_y(y), m_x_rel(x_rel), m_y_rel(y_rel), 13 | m_buttons { left, right, middle } 14 | { 15 | } 16 | 17 | MouseMotionEvent::MotionState 18 | MouseMotionEvent::state(MouseMotionEvent::Button button) const 19 | { 20 | return m_buttons[button]; 21 | } 22 | 23 | int 24 | MouseMotionEvent::x() const 25 | { 26 | return m_x; 27 | } 28 | 29 | int 30 | MouseMotionEvent::y() const 31 | { 32 | return m_y; 33 | } 34 | 35 | int 36 | MouseMotionEvent::x_rel() const 37 | { 38 | return m_x_rel; 39 | } 40 | 41 | int 42 | MouseMotionEvent::y_rel() const 43 | { 44 | return m_y_rel; 45 | } 46 | 47 | MouseMotionEvent 48 | MouseMotionEvent::from_SDL(const SDL_Event& event) 49 | { 50 | int x = event.motion.x; 51 | int y = event.motion.y; 52 | int xrel = event.motion.xrel; 53 | int yrel = event.motion.yrel; 54 | 55 | Uint8 state = event.motion.state; 56 | MotionState left = UP, right = UP, middle = UP; 57 | 58 | if (state & SDL_BUTTON(1)) 59 | { 60 | left = DOWN; 61 | } 62 | 63 | if (state & SDL_BUTTON(2)) 64 | { 65 | middle = DOWN; 66 | } 67 | 68 | if (state & SDL_BUTTON(3)) 69 | { 70 | right = DOWN; 71 | } 72 | 73 | return MouseMotionEvent(x, y, xrel, yrel, left, right, middle); 74 | } 75 | -------------------------------------------------------------------------------- /engine/src/music.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Music. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/music.h" 9 | 10 | class Music::Impl 11 | { 12 | public: 13 | Impl(Mix_Music* const music) 14 | : m_mix_music{music} 15 | { 16 | } 17 | 18 | ~Impl() 19 | { 20 | if (m_mix_music != nullptr) 21 | { 22 | Mix_FreeMusic(m_mix_music); 23 | } 24 | } 25 | 26 | Mix_Music * mix_music() const { return m_mix_music; }; 27 | 28 | private: 29 | Mix_Music* const m_mix_music; 30 | }; 31 | 32 | Music::Music(Mix_Music* const music) 33 | : m_impl{new Impl(music)} 34 | { 35 | } 36 | 37 | Music::~Music() 38 | { 39 | } 40 | 41 | Mix_Music* 42 | Music::mix_music() const 43 | { 44 | return m_impl->mix_music(); 45 | } 46 | 47 | Music * 48 | Music::from_file(const string& path) throw (Exception) 49 | { 50 | Mix_Music *music = Mix_LoadMUS(path.c_str()); 51 | 52 | if (music == nullptr) 53 | { 54 | throw Exception(Mix_GetError()); 55 | } 56 | 57 | Music *m = new Music(music); 58 | 59 | if (not m) 60 | { 61 | throw Exception("Out of memory for a new Music"); 62 | } 63 | 64 | return m; 65 | } 66 | -------------------------------------------------------------------------------- /engine/src/point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um ponto no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/point.h" 9 | 10 | Point::Point(double x, double y) 11 | : m_x(x), m_y(y) 12 | { 13 | } 14 | 15 | double 16 | Point::x() const 17 | { 18 | return m_x; 19 | } 20 | 21 | double 22 | Point::y() const 23 | { 24 | return m_y; 25 | } 26 | 27 | void 28 | Point::set_x(double x) 29 | { 30 | m_x = x; 31 | } 32 | 33 | void 34 | Point::set_y(double y) 35 | { 36 | m_y = y; 37 | } 38 | 39 | void 40 | Point::set(double x, double y) 41 | { 42 | m_x = x; 43 | m_y = y; 44 | } 45 | -------------------------------------------------------------------------------- /engine/src/rect.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da lasse que representa um retângulo no espaço bidimensional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/rect.h" 9 | 10 | #include 11 | 12 | using std::min; 13 | using std::max; 14 | 15 | Rect::Rect(double x, double y, double w, double h) 16 | : m_x(x), m_y(y), m_w(w), m_h(h) 17 | { 18 | } 19 | 20 | double 21 | Rect::x() const 22 | { 23 | return m_x; 24 | } 25 | 26 | double 27 | Rect::y() const 28 | { 29 | return m_y; 30 | } 31 | 32 | double 33 | Rect::w() const 34 | { 35 | return m_w; 36 | } 37 | 38 | double 39 | Rect::h() const 40 | { 41 | return m_h; 42 | } 43 | 44 | void 45 | Rect::set_x(double x) 46 | { 47 | m_x = x; 48 | } 49 | 50 | void 51 | Rect::set_y(double y) 52 | { 53 | m_y = y; 54 | } 55 | 56 | void 57 | Rect::set_w(double w) 58 | { 59 | m_w = w; 60 | } 61 | 62 | void 63 | Rect::set_h(double h) 64 | { 65 | m_h = h; 66 | } 67 | 68 | void 69 | Rect::set_position(double x, double y) 70 | { 71 | m_x = x; 72 | m_y = y; 73 | } 74 | 75 | void 76 | Rect::set_dimensions(double w, double h) 77 | { 78 | m_w = w; 79 | m_h = h; 80 | } 81 | 82 | bool 83 | Rect::contains(double x, double y) const 84 | { 85 | return x >= m_x and x <= m_x + m_w and y >= m_y and y <= m_y + m_h; 86 | } 87 | 88 | Rect 89 | Rect::intersection(const Rect& r) const 90 | { 91 | typedef struct _Area 92 | { 93 | double left, right; 94 | double top, bottom; 95 | } Area; 96 | 97 | Area a { x(), x() + w(), y(), y() + h() }; 98 | Area b { r.x(), r.x() + r.w(), r.y(), r.y() + r.h() }; 99 | 100 | if (b.right < a.left or b.left > a.right or b.top > a.bottom 101 | or b.bottom < a.top) 102 | { 103 | return Rect(0, 0, 0, 0); 104 | } 105 | else 106 | { 107 | double xmin = (max)(a.left, b.left); 108 | double xmax = (min)(a.right, b.right); 109 | double ymin = (max)(a.top, b.top); 110 | double ymax = (min)(a.bottom, b.bottom); 111 | 112 | return Rect(xmin, ymin, xmax - xmin, ymax - ymin); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /engine/src/resourcesmanager.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um gerenciador de recursos 3 | * reutilizáveis. 4 | * 5 | * Autor: Edson Alves 6 | * Data: 20/04/2015 7 | * Licença: LGPL. Sem copyright. 8 | */ 9 | #include "core/resourcesmanager.h" 10 | 11 | #include "core/texture.h" 12 | #include "core/font.h" 13 | #include "core/music.h" 14 | #include "core/soundeffect.h" 15 | #include "core/bitmap.h" 16 | #include "core/settings.h" 17 | 18 | shared_ptr 19 | ResourcesManager::get_texture(const string& id) throw (Exception) 20 | { 21 | if (m_textures.find(id) != m_textures.end()) 22 | { 23 | return m_textures[id]; 24 | } 25 | 26 | return acquire_texture(id); 27 | } 28 | 29 | shared_ptr 30 | ResourcesManager::acquire_texture(const string& id) throw (Exception) 31 | { 32 | Texture *texture = Texture::from_file(id); 33 | 34 | if (not texture) 35 | { 36 | throw Exception("Can't load texture " + id); 37 | } 38 | 39 | shared_ptr ptr(texture); 40 | m_textures[id] = ptr; 41 | 42 | return ptr; 43 | } 44 | 45 | shared_ptr 46 | ResourcesManager::get_font(const string& id) throw (Exception) 47 | { 48 | if (m_fonts.find(id) != m_fonts.end()) 49 | { 50 | return m_fonts[id]; 51 | } 52 | 53 | return acquire_font(id); 54 | } 55 | 56 | shared_ptr 57 | ResourcesManager::acquire_font(const string& id) throw (Exception) 58 | { 59 | Font *font = Font::from_file(id); 60 | 61 | if (not font) 62 | { 63 | throw Exception("Can't load font " + id); 64 | } 65 | 66 | shared_ptr ptr(font); 67 | m_fonts[id] = ptr; 68 | 69 | return ptr; 70 | } 71 | 72 | shared_ptr 73 | ResourcesManager::get_music(const string& id) throw (Exception) 74 | { 75 | if (m_musics.find(id) != m_musics.end()) 76 | { 77 | return m_musics[id]; 78 | } 79 | 80 | return acquire_music(id); 81 | } 82 | 83 | shared_ptr 84 | ResourcesManager::acquire_music(const string& id) throw (Exception) 85 | { 86 | Music *music = Music::from_file(id); 87 | 88 | if (not music) 89 | { 90 | throw Exception("Can't load music " + id); 91 | } 92 | 93 | shared_ptr ptr(music); 94 | m_musics[id] = ptr; 95 | 96 | return ptr; 97 | } 98 | 99 | shared_ptr 100 | ResourcesManager::get_sound_effect(const string& id) throw (Exception) 101 | { 102 | if (m_sound_effects.find(id) != m_sound_effects.end()) 103 | { 104 | return m_sound_effects[id]; 105 | } 106 | 107 | return acquire_sound_effect(id); 108 | } 109 | 110 | shared_ptr 111 | ResourcesManager::acquire_sound_effect(const string& id) throw (Exception) 112 | { 113 | SoundEffect *sfx = SoundEffect::from_file(id); 114 | 115 | if (not sfx) 116 | { 117 | throw Exception("Can't load sound effect " + id); 118 | } 119 | 120 | shared_ptr ptr(sfx); 121 | m_sound_effects[id] = ptr; 122 | 123 | return ptr; 124 | } 125 | 126 | shared_ptr 127 | ResourcesManager::get_bitmap(const string& id) throw (Exception) 128 | { 129 | if (m_bitmaps.find(id) != m_bitmaps.end()) 130 | { 131 | return m_bitmaps[id]; 132 | } 133 | 134 | return acquire_bitmap(id); 135 | } 136 | 137 | shared_ptr 138 | ResourcesManager::get_settings(const string& id) throw (Exception) 139 | { 140 | Settings *settings = Settings::from_file(id); 141 | 142 | if (not settings) 143 | { 144 | settings = new Settings(); 145 | } 146 | 147 | shared_ptr ptr(settings); 148 | m_settings[id] = ptr; 149 | 150 | return ptr; 151 | } 152 | 153 | shared_ptr 154 | ResourcesManager::acquire_bitmap(const string& id) throw (Exception) 155 | { 156 | Bitmap *bitmap = Bitmap::from_file(id); 157 | 158 | if (not bitmap) 159 | { 160 | throw Exception("Can't load bitmap " + id); 161 | } 162 | 163 | shared_ptr ptr(bitmap); 164 | m_bitmaps[id] = ptr; 165 | 166 | return ptr; 167 | } 168 | 169 | void 170 | ResourcesManager::scale(double k) 171 | { 172 | for (auto t : m_textures) 173 | { 174 | shared_ptr texture = t.second; 175 | texture->scale(k); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /engine/src/settings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Settings. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/settings.h" 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | typedef enum {NONE, COMMENT, SECTION, NAME, VALUE} State; 15 | 16 | string 17 | ltrim(const string& s) 18 | { 19 | int pos; 20 | 21 | for (pos = 0; s[pos] == ' '; ++pos) {} 22 | 23 | return s.substr(pos); 24 | } 25 | 26 | Settings * 27 | Settings::from_file(const string& path) throw (Exception) 28 | { 29 | Settings *settings = new Settings(); 30 | 31 | ifstream inifile(path); 32 | 33 | if (not inifile) 34 | { 35 | return settings; 36 | } 37 | 38 | State state = NONE; 39 | char c; 40 | ostringstream os; 41 | string section = "", name, value; 42 | 43 | while (not inifile.eof()) 44 | { 45 | c = inifile.get(); 46 | 47 | switch (state) 48 | { 49 | case NONE: 50 | if (c == ';') 51 | { 52 | state = COMMENT; 53 | } 54 | else if (c == '[') 55 | { 56 | state = SECTION; 57 | } 58 | else if (isalpha(c) && section != "") 59 | { 60 | state = NAME; 61 | os << c; 62 | } 63 | 64 | break; 65 | 66 | case COMMENT: 67 | if (c == '\n') 68 | { 69 | state = NONE; 70 | } 71 | 72 | break; 73 | 74 | case SECTION: 75 | if (c == '\n') 76 | { 77 | throw Exception("Invalid syntax!"); 78 | } 79 | 80 | if (c != ']') 81 | { 82 | os << c; 83 | } 84 | else 85 | { 86 | section = os.str(); 87 | os.str(""); 88 | state = NONE; 89 | } 90 | 91 | break; 92 | 93 | case NAME: 94 | if (c == '\n') 95 | { 96 | throw Exception("Missing value!"); 97 | } 98 | 99 | if (isalnum(c) || c == '_') 100 | { 101 | os << c; 102 | } 103 | else if (c == '=') 104 | { 105 | if (section == "") 106 | { 107 | throw Exception("Missing section!"); 108 | } 109 | 110 | name = os.str(); 111 | os.str(""); 112 | state = VALUE; 113 | } 114 | 115 | break; 116 | 117 | case VALUE: 118 | if (c != '\n') 119 | { 120 | os << c; 121 | } 122 | else 123 | { 124 | value = os.str(); 125 | settings->write(section, name, value); 126 | os.str(""); 127 | state = NONE; 128 | } 129 | 130 | break; 131 | } 132 | } 133 | 134 | if (state == VALUE) 135 | { 136 | settings->write(section, name, value); 137 | } 138 | 139 | return settings; 140 | } 141 | 142 | void 143 | Settings::save(const string& path) throw (Exception) 144 | { 145 | ofstream outfile(path); 146 | 147 | if (not outfile) 148 | { 149 | throw Exception("Settings::save(): Couldn't open file " + path); 150 | } 151 | 152 | for (auto it : m_sections) 153 | { 154 | outfile << endl << "[" << it.first << "]" << endl << endl; 155 | 156 | for (auto p : it.second) 157 | { 158 | outfile << p.first << "=" << p.second << endl; 159 | } 160 | } 161 | 162 | outfile.close(); 163 | } 164 | 165 | map< string, map > 166 | Settings::sections() const 167 | { 168 | return m_sections; 169 | } 170 | -------------------------------------------------------------------------------- /engine/src/soundeffect.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe AudioHandler. 3 | * 4 | * Autor: Caio Nardelli 5 | * Data: 17/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/soundeffect.h" 9 | 10 | class SoundEffect::Impl 11 | { 12 | public: 13 | Impl(Mix_Chunk* const soundeffect) 14 | : m_mix_chunk{soundeffect}, m_channel{-1} 15 | { 16 | } 17 | 18 | ~Impl() 19 | { 20 | if (m_mix_chunk != nullptr) 21 | { 22 | Mix_FreeChunk(m_mix_chunk); 23 | } 24 | } 25 | 26 | Mix_Chunk * mix_chunk() const { return m_mix_chunk; } 27 | int channel() const { return m_channel; } 28 | void set_channel(const int channel) { m_channel = channel; } 29 | 30 | private: 31 | Mix_Chunk* const m_mix_chunk; 32 | int m_channel; 33 | }; 34 | 35 | SoundEffect::SoundEffect(Mix_Chunk* const soundeffect) 36 | : m_impl(new Impl(soundeffect)) 37 | { 38 | } 39 | 40 | SoundEffect::~SoundEffect() 41 | { 42 | } 43 | 44 | Mix_Chunk* 45 | SoundEffect::mix_chunk() const 46 | { 47 | return m_impl->mix_chunk(); 48 | } 49 | 50 | int 51 | SoundEffect::channel() const 52 | { 53 | return m_impl->channel(); 54 | } 55 | 56 | void 57 | SoundEffect::set_channel(const int channel) 58 | { 59 | m_impl->set_channel(channel); 60 | } 61 | 62 | SoundEffect * 63 | SoundEffect::from_file(const string& path) throw (Exception) 64 | { 65 | Mix_Chunk *soundeffect = Mix_LoadWAV(path.c_str()); 66 | 67 | if (soundeffect == nullptr) 68 | { 69 | throw Exception(Mix_GetError()); 70 | } 71 | 72 | SoundEffect *sfx = new SoundEffect(soundeffect); 73 | 74 | if (not sfx) 75 | { 76 | throw Exception("Out of memory for a new SoundEffect"); 77 | } 78 | 79 | return sfx; 80 | } 81 | -------------------------------------------------------------------------------- /engine/src/sprite.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Sprite. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 05/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/rect.h" 9 | #include "core/sprite.h" 10 | #include "core/keyboardevent.h" 11 | #include "core/environment.h" 12 | 13 | using std::make_pair; 14 | 15 | class Sprite::Impl 16 | { 17 | public: 18 | Impl(Sprite *sprite) 19 | : m_sprite(sprite), m_state_id(0), m_state(nullptr) 20 | { 21 | } 22 | 23 | ~Impl() 24 | { 25 | } 26 | 27 | void report_event(int event) 28 | { 29 | auto it = m_fsm.find(make_pair(m_state_id, event)); 30 | 31 | if (it == m_fsm.end()) 32 | { 33 | return; 34 | } 35 | 36 | int next = it->second; 37 | 38 | change_state(next, m_state_id); 39 | } 40 | 41 | void change_state(int to, int from) 42 | { 43 | auto it = m_states.find(to); 44 | 45 | if (it == m_states.end()) 46 | { 47 | return; 48 | } 49 | 50 | if (m_state) 51 | { 52 | m_state->leave(to); 53 | } 54 | 55 | m_state_id = to; 56 | m_state = it->second; 57 | 58 | if (m_state) 59 | { 60 | m_state->enter(from); 61 | } 62 | } 63 | 64 | void add_state(int id, SpriteState *state) 65 | { 66 | m_states[id] = state; 67 | } 68 | 69 | void add_transition(int event, int from, int to) 70 | { 71 | m_fsm[make_pair(from, event)] = to; 72 | } 73 | 74 | void update_self(unsigned long elapsed) 75 | { 76 | if (m_state) 77 | { 78 | m_state->update(elapsed); 79 | } 80 | } 81 | 82 | void draw_self() 83 | { 84 | if (m_state) 85 | { 86 | m_state->draw(); 87 | } 88 | } 89 | 90 | SpriteState * state() const 91 | { 92 | return m_state; 93 | } 94 | 95 | bool on_event(const KeyboardEvent& event) 96 | { 97 | if (m_state) 98 | { 99 | return m_state->on_event(event); 100 | } 101 | 102 | return false; 103 | } 104 | 105 | bool on_event(const MouseButtonEvent& event) 106 | { 107 | if (m_state) 108 | { 109 | return m_state->on_event(event); 110 | } 111 | 112 | return false; 113 | } 114 | 115 | bool on_event(const MouseMotionEvent& event) 116 | { 117 | if (m_state) 118 | { 119 | return m_state->on_event(event); 120 | } 121 | 122 | return false; 123 | } 124 | 125 | bool on_event(const JoyStickEvent& event) 126 | { 127 | if (m_state) 128 | { 129 | return m_state->on_event(event); 130 | } 131 | 132 | return false; 133 | } 134 | 135 | private: 136 | Sprite *m_sprite; 137 | int m_state_id; 138 | SpriteState *m_state; 139 | map< pair, int > m_fsm; 140 | map m_states; 141 | }; 142 | 143 | Sprite::Sprite(Object *parent, ObjectID id) 144 | : Object(parent, id), m_impl(new Impl(this)) 145 | { 146 | } 147 | 148 | Sprite::~Sprite() 149 | { 150 | } 151 | 152 | void 153 | Sprite::report_event(int event) 154 | { 155 | m_impl->report_event(event); 156 | } 157 | 158 | void 159 | Sprite::change_state(int to, int from) 160 | { 161 | m_impl->change_state(to, from); 162 | } 163 | 164 | void 165 | Sprite::add_state(int id, SpriteState *state) 166 | { 167 | m_impl->add_state(id, state); 168 | } 169 | 170 | void 171 | Sprite::add_transition(int event, int from, int to) 172 | { 173 | m_impl->add_transition(event, from, to); 174 | } 175 | 176 | void 177 | Sprite::update_self(unsigned long elapsed) 178 | { 179 | m_impl->update_self(elapsed); 180 | } 181 | 182 | void 183 | Sprite::draw_self() 184 | { 185 | m_impl->draw_self(); 186 | } 187 | 188 | SpriteState * 189 | Sprite::state() const 190 | { 191 | return m_impl->state(); 192 | } 193 | 194 | bool 195 | Sprite::on_event(const KeyboardEvent& event) 196 | { 197 | return m_impl->on_event(event); 198 | } 199 | 200 | bool 201 | Sprite::on_event(const MouseButtonEvent& event) 202 | { 203 | return m_impl->on_event(event); 204 | } 205 | 206 | bool 207 | Sprite::on_event(const MouseMotionEvent& event) 208 | { 209 | return m_impl->on_event(event); 210 | } 211 | 212 | bool 213 | Sprite::on_event(const JoyStickEvent& event) 214 | { 215 | return m_impl->on_event(event); 216 | } 217 | -------------------------------------------------------------------------------- /engine/src/systemevent.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um evento do sistema operacional. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 22/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/systemevent.h" 9 | 10 | SystemEvent::SystemEvent(SystemEvent::Type type) 11 | : m_type(type) 12 | { 13 | } 14 | 15 | SystemEvent::Type 16 | SystemEvent::type() const 17 | { 18 | return m_type; 19 | } 20 | 21 | SystemEvent 22 | SystemEvent::from_SDL(const SDL_Event& event) 23 | { 24 | if (event.type == SDL_QUIT) 25 | { 26 | return SystemEvent(SystemEvent::QUIT); 27 | } 28 | 29 | return SystemEvent(SystemEvent::UNKNOWN); 30 | } 31 | -------------------------------------------------------------------------------- /engine/src/text.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa um texto. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 14/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/text.h" 9 | 10 | #include "core/font.h" 11 | #include "core/texture.h" 12 | #include "core/environment.h" 13 | 14 | #include 15 | 16 | class Text::Impl 17 | { 18 | public: 19 | Impl(Text *parent, const string& text, const Color& color) 20 | : m_parent(parent) 21 | { 22 | Environment *env = Environment::get_instance(); 23 | m_texture = env->canvas->render_text(text, color); 24 | 25 | if (m_texture) 26 | { 27 | parent->set_dimensions(m_texture->w(), m_texture->h()); 28 | } 29 | } 30 | 31 | ~Impl() 32 | { 33 | delete m_texture; 34 | } 35 | 36 | void draw_self() 37 | { 38 | if (not m_texture) 39 | { 40 | return; 41 | } 42 | 43 | Environment *env = Environment::get_instance(); 44 | env->canvas->draw(m_texture, m_parent->x(), m_parent->y()); 45 | } 46 | 47 | private: 48 | Text *m_parent; 49 | Texture *m_texture; 50 | }; 51 | 52 | Text::Text(Object *parent, const string& texture, const Color& color) 53 | : Object(parent, texture), m_impl(new Impl(this, texture, color)) 54 | { 55 | } 56 | 57 | Text::~Text() 58 | { 59 | } 60 | 61 | void 62 | Text::draw_self() 63 | { 64 | m_impl->draw_self(); 65 | } 66 | -------------------------------------------------------------------------------- /engine/src/texture.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe que representa uma textura. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 13/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/texture.h" 9 | #include "core/exception.h" 10 | #include "core/environment.h" 11 | #include "core/settings.h" 12 | 13 | #include 14 | #include 15 | 16 | using std::make_pair; 17 | 18 | class Texture::Impl 19 | { 20 | public: 21 | Impl(void *data, int w, int h) 22 | : m_w(w), m_h(h) 23 | { 24 | m_texture = static_cast(data); 25 | 26 | Environment *env = Environment::get_instance(); 27 | shared_ptr settings = env->resources_manager->get_settings(env->m_settings_path); 28 | double k = settings->read("Game", "scale", 1); 29 | 30 | scale(k); 31 | } 32 | 33 | ~Impl() 34 | { 35 | if (m_texture) 36 | { 37 | SDL_DestroyTexture(m_texture); 38 | } 39 | } 40 | 41 | int w() const { return m_w; } 42 | int h() const { return m_h; } 43 | void * data() const { return m_texture; } 44 | 45 | void scale(double k) 46 | { 47 | m_w = size().first * k; 48 | m_h = size().second * k; 49 | } 50 | 51 | pair size() const 52 | { 53 | int w, h; 54 | int rc = SDL_QueryTexture(m_texture, nullptr, nullptr, &w, &h); 55 | 56 | if (rc) 57 | { 58 | throw Exception(SDL_GetError()); 59 | } 60 | 61 | return make_pair(w, h); 62 | } 63 | 64 | private: 65 | int m_w, m_h; 66 | SDL_Texture *m_texture; 67 | }; 68 | 69 | Texture::Texture(void *data, int w, int h) 70 | : m_impl(new Impl(data, w, h)) 71 | { 72 | } 73 | 74 | Texture::~Texture() 75 | { 76 | } 77 | 78 | void * 79 | Texture::data() const 80 | { 81 | return m_impl->data(); 82 | } 83 | 84 | int 85 | Texture::w() const 86 | { 87 | return m_impl->w(); 88 | } 89 | 90 | int 91 | Texture::h() const 92 | { 93 | return m_impl->h(); 94 | } 95 | 96 | Texture * 97 | Texture::from_file(const string& path) throw (Exception) 98 | { 99 | Environment *env = Environment::get_instance(); 100 | SDL_Renderer *renderer = env->canvas->renderer(); 101 | SDL_Texture *texture = IMG_LoadTexture(renderer, path.c_str()); 102 | 103 | if (texture == nullptr) 104 | { 105 | throw Exception(SDL_GetError()); 106 | } 107 | 108 | int w, h; 109 | 110 | int rc = SDL_QueryTexture(texture, nullptr, nullptr, &w, &h); 111 | 112 | if (rc) 113 | { 114 | throw Exception(SDL_GetError()); 115 | } 116 | 117 | Texture *t = new Texture(texture, w, h); 118 | 119 | 120 | if (not t) 121 | { 122 | throw Exception("Out of memory for a new Texture"); 123 | } 124 | 125 | return t; 126 | } 127 | 128 | void 129 | Texture::scale(double k) 130 | { 131 | m_impl->scale(k); 132 | } 133 | 134 | pair 135 | Texture::size() const 136 | { 137 | return m_impl->size(); 138 | } 139 | -------------------------------------------------------------------------------- /engine/src/video.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Video. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 06/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "core/video.h" 9 | #include "core/camera.h" 10 | #include "core/canvas.h" 11 | #include "core/environment.h" 12 | 13 | #include 14 | 15 | using std::make_pair; 16 | 17 | Video::Video() 18 | : m_window(nullptr), m_renderer(nullptr), m_canvas(nullptr), 19 | m_camera(nullptr), m_w(800), m_h(600) 20 | { 21 | } 22 | 23 | Video::~Video() 24 | { 25 | if (m_camera) 26 | { 27 | delete m_camera; 28 | } 29 | 30 | if (m_canvas) 31 | { 32 | delete m_canvas; 33 | } 34 | 35 | if (m_renderer) 36 | { 37 | SDL_DestroyRenderer(m_renderer); 38 | } 39 | 40 | if (m_window) 41 | { 42 | SDL_DestroyWindow(m_window); 43 | } 44 | 45 | if (TTF_WasInit()) 46 | { 47 | TTF_Quit(); 48 | } 49 | 50 | if (SDL_WasInit(SDL_INIT_EVERYTHING)) 51 | { 52 | SDL_Quit(); 53 | } 54 | } 55 | 56 | void 57 | Video::init() throw (Exception) 58 | { 59 | int rc = SDL_Init(SDL_INIT_VIDEO); 60 | 61 | if (rc) 62 | { 63 | throw Exception(SDL_GetError()); 64 | } 65 | 66 | rc = TTF_Init(); 67 | 68 | if (rc) 69 | { 70 | throw Exception(TTF_GetError()); 71 | } 72 | 73 | rc = SDL_CreateWindowAndRenderer(m_w, m_h, 0, &m_window, &m_renderer); 74 | 75 | if (rc or not m_window or not m_renderer) 76 | { 77 | throw Exception(SDL_GetError()); 78 | } 79 | 80 | m_canvas = new Canvas(m_renderer, m_w, m_h); 81 | 82 | if (not m_canvas) 83 | { 84 | throw Exception("Out of memory for a new Canvas"); 85 | } 86 | 87 | m_camera = new Camera(0, 0, m_w, m_h); 88 | 89 | if (not m_camera) 90 | { 91 | throw Exception("Out of memory for a new Camera"); 92 | } 93 | } 94 | 95 | void 96 | Video::set_resolution(int w, int h, double scale) throw (Exception) 97 | { 98 | if (m_window and w > 0 and h > 0) 99 | { 100 | m_w = w; 101 | m_h = h; 102 | SDL_SetWindowSize(m_window, w, h); 103 | 104 | int rc = SDL_RenderSetLogicalSize(m_renderer, m_w, m_h); 105 | 106 | if (rc != 0) 107 | { 108 | throw Exception(SDL_GetError()); 109 | } 110 | 111 | m_canvas->set_resolution(w, h); 112 | m_camera->set_dimensions(w, h); 113 | 114 | Environment *env = Environment::get_instance(); 115 | env->resources_manager->scale(scale); 116 | } 117 | } 118 | 119 | void 120 | Video::set_fullscreen(bool fullscreen) throw (Exception) 121 | { 122 | if (not m_window) 123 | { 124 | return; 125 | } 126 | 127 | int flag = fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0; 128 | 129 | int rc = SDL_SetWindowFullscreen(m_window, flag); 130 | 131 | if (rc != 0) 132 | { 133 | throw Exception(SDL_GetError()); 134 | } 135 | 136 | } 137 | 138 | bool 139 | Video::fullscreen() const 140 | { 141 | int flag = SDL_GetWindowFlags(m_window); 142 | 143 | if (flag & SDL_WINDOW_FULLSCREEN) 144 | { 145 | return true; 146 | } 147 | 148 | return false; 149 | } 150 | 151 | void 152 | Video::set_window_name(const string& name) 153 | { 154 | if (m_window) 155 | { 156 | SDL_SetWindowTitle(m_window, name.c_str()); 157 | } 158 | } 159 | 160 | pair 161 | Video::resolution() const 162 | { 163 | return make_pair(m_w, m_h); 164 | } 165 | 166 | Canvas * 167 | Video::canvas() const 168 | { 169 | return m_canvas; 170 | } 171 | 172 | Camera * 173 | Video::camera() const 174 | { 175 | return m_camera; 176 | } 177 | -------------------------------------------------------------------------------- /engine/test/dinoman.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Classe que representa o sprite do teste. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 15/05/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef DINOMAN_H 9 | #define DINOMAN_H 10 | 11 | #include "core/sprite.h" 12 | 13 | using std::pair; 14 | 15 | class Dinoman : public Sprite 16 | { 17 | public: 18 | typedef enum { NONE, IDLE, RUNNING } State; 19 | typedef enum { MOVED, STOPPED } Event; 20 | typedef enum { LEFT, RIGHT } Direction; 21 | 22 | Dinoman(Object *parent, const string& id); 23 | ~Dinoman(); 24 | 25 | Direction direction() const; 26 | void set_direction(Direction direction); 27 | 28 | const pair& moviment() const; 29 | void set_moviment(double xaxis, double yaxis); 30 | 31 | private: 32 | class Impl; 33 | unique_ptr m_impl; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /engine/test/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Teste elementar das funcionalidades da biblioteca. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 26/03/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "test.h" 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | #include "core/settings.h" 15 | 16 | int main(int, char *[]) 17 | { 18 | try 19 | { 20 | Test test; 21 | test.init("temp.ini"); 22 | test.run(); 23 | } catch (Exception ex) 24 | { 25 | cerr << ex.message() << endl; 26 | return -1; 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /engine/test/stage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da class FrontEnd. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "stage.h" 9 | #include "core/font.h" 10 | #include "dinoman.h" 11 | #include "core/environment.h" 12 | 13 | #include 14 | 15 | using std::map; 16 | 17 | Stage::Stage(ObjectID id) 18 | : Level(id) 19 | { 20 | Environment *env = Environment::get_instance(); 21 | 22 | double x = -1000; 23 | double y = env->canvas->h()*0.6; 24 | double w = 3500; 25 | double h = env->canvas->h(); 26 | 27 | m_floor.set_position(x, y); 28 | m_floor.set_dimensions(w, h); 29 | 30 | Dinoman *dinoman = new Dinoman(this, "dinoman"); 31 | 32 | y -= dinoman->h(); 33 | 34 | dinoman->set_position(x, y); 35 | add_child(dinoman); 36 | 37 | env->camera->set_mode(Camera::FOLLOWING); 38 | env->camera->follow(dinoman); 39 | env->camera->set_limits(Rect(-1000, -1000, 3500, 2000)); 40 | 41 | shared_ptr font = env->resources_manager->get_font("res/fonts/FLATS.ttf"); 42 | 43 | font->set_size(80); 44 | font->set_style(Font::ITALIC); 45 | env->canvas->set_font(font); 46 | } 47 | 48 | void 49 | Stage::draw_self() 50 | { 51 | const Color color { 0, 200, 60 }, gray { 20, 20, 20 }; 52 | 53 | Environment *env = Environment::get_instance(); 54 | env->canvas->clear(Color::WHITE); 55 | env->canvas->fill(m_floor, color); 56 | 57 | env->canvas->draw("Scrolling test: 1 2 3 4 5 6 7 8 9 10", 150, 500, gray); 58 | } 59 | -------------------------------------------------------------------------------- /engine/test/stage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Exemplo de um nível: a tela de uma fase. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 29/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef STAGE_H 9 | #define STAGE_H 10 | 11 | #include "core/level.h" 12 | #include "core/rect.h" 13 | 14 | class Stage : public Level 15 | { 16 | public: 17 | Stage(ObjectID id); 18 | 19 | private: 20 | Rect m_floor; 21 | 22 | void draw_self(); 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /engine/test/test.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe Test. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "test.h" 9 | #include "util/frontend.h" 10 | #include "titlescreen.h" 11 | #include "stage.h" 12 | 13 | Test::Test() 14 | : Game("a") 15 | { 16 | } 17 | 18 | Level * 19 | Test::load_level(const string& id) 20 | { 21 | if (id == "a") 22 | { 23 | return new FrontEnd("a", "b", "res/images/hexagon.png"); 24 | } 25 | else if (id == "b") 26 | { 27 | return new FrontEnd("b", "c", "res/images/star.png"); 28 | } 29 | else if (id == "c") 30 | { 31 | return new FrontEnd("c", "title", "res/images/spiral.png"); 32 | } else if (id == "title") 33 | { 34 | return new TitleScreen(); 35 | } else if (id == "stage1") 36 | { 37 | return new Stage("stage1"); 38 | } 39 | 40 | return nullptr; 41 | } 42 | -------------------------------------------------------------------------------- /engine/test/test.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Exemplo de um jogo. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 20/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #ifndef TEST_H 9 | #define TEST_H 10 | 11 | #include "core/game.h" 12 | 13 | class Test : public Game 14 | { 15 | public: 16 | Test(); 17 | 18 | private: 19 | Level * load_level(const string& id); 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /engine/test/titlescreen.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Implementação da classe TitleScreen. 3 | * 4 | * Autor: Edson Alves 5 | * Data: 29/04/2015 6 | * Licença: LGPL. Sem copyright. 7 | */ 8 | #include "titlescreen.h" 9 | #include "core/environment.h" 10 | #include "core/font.h" 11 | #include "util/button.h" 12 | 13 | using namespace std; 14 | 15 | TitleScreen::TitleScreen() 16 | : Level("title") 17 | { 18 | Environment *env = Environment::get_instance(); 19 | 20 | shared_ptr font = env->resources_manager->get_font("res/fonts/FLATS.ttf"); 21 | 22 | font->set_size(40); 23 | font->set_style(Font::BOLD); 24 | env->canvas->set_font(font); 25 | 26 | double w = env->canvas->w(); 27 | double h = env->canvas->h(); 28 | 29 | set_dimensions(w, h); 30 | 31 | double bw = 250; 32 | double bh = 100; 33 | 34 | Button *ok = new Button(this, "ok", bw, bh); 35 | ok->set_text("Start", Color::WHITE); 36 | ok->set_color(Color::BLUE, Color::GREEN); 37 | ok->align_to(this, Object::CENTER, Object::NONE); 38 | ok->set_y(h*0.4); 39 | ok->set_border(5); 40 | 41 | Button *exit = new Button(this, "exit", bw, bh); 42 | exit->set_text("Quit", Color(0, 0, 0, 32)); 43 | exit->set_color(Color(255, 255, 0, 128), Color::RED); 44 | exit->align_to(this, Object::CENTER, Object::NONE); 45 | exit->set_y(ok->y() + ok->h() + 20); 46 | 47 | ok->add_observer(this); 48 | exit->add_observer(this); 49 | 50 | add_child(ok); 51 | add_child(exit); 52 | } 53 | 54 | TitleScreen::~TitleScreen() 55 | { 56 | } 57 | 58 | void 59 | TitleScreen::draw_self() 60 | { 61 | Environment *env = Environment::get_instance(); 62 | env->canvas->clear(Color::WHITE); 63 | } 64 | 65 | bool 66 | TitleScreen::on_message(Object *object, MessageID id, Parameters) 67 | { 68 | if (id != Button::clickedID) 69 | { 70 | return false; 71 | } 72 | 73 | Button *button = dynamic_cast