diff --git a/Makefile b/Makefile index f8c11ab72..9da5a4bc8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=g++ CFLAGS=-c -Wall LDFLAGS=-lSDL -SRCSOURCES=main.cpp Renderer.cpp GuiComponent.cpp +SRCSOURCES=main.cpp Renderer.cpp Renderer_draw.cpp GuiComponent.cpp components/GuiTitleScreen.cpp SOURCES=$(addprefix src/,$(SRCSOURCES)) OBJECTS=$(SOURCES:.cpp=.o) EXECUTABLE=emulationstation @@ -15,4 +15,4 @@ $(EXECUTABLE): $(OBJECTS) $(CC) $(CFLAGS) $< -o $@ clean: - rm -rf src/*o $(EXECUTABLE) + rm -rf src/*o src/components/*o $(EXECUTABLE) diff --git a/src/GuiComponent.cpp b/src/GuiComponent.cpp index e172a4e16..9a1c950a9 100644 --- a/src/GuiComponent.cpp +++ b/src/GuiComponent.cpp @@ -1,12 +1,32 @@ #include "GuiComponent.h" #include "Renderer.h" +#include -GuiComponent::GuiComponent() +void GuiComponent::addChild(GuiComponent* comp) { - Renderer::registerComponent(this); + mChildren.push_back(comp); } -GuiComponent::~GuiComponent() +void GuiComponent::removeChild(GuiComponent* comp) { - Renderer::unregisterComponent(this); + for(unsigned int i = 0; i < mChildren.size(); i++) + { + if(mChildren.at(i) == comp) + { + mChildren.erase(mChildren.begin() + i); + break; + } + } + + std::cerr << "Error - tried to remove GuiComponent child, but couldn't find it!\n"; +} + +void GuiComponent::render() +{ + onRender(); + + for(unsigned int i = 0; i < mChildren.size(); i++) + { + mChildren.at(i)->render(); + } } diff --git a/src/GuiComponent.h b/src/GuiComponent.h index 786671bed..6219ce656 100644 --- a/src/GuiComponent.h +++ b/src/GuiComponent.h @@ -1,13 +1,22 @@ #ifndef _GUICOMPONENT_H_ #define _GUICOMPONENT_H_ +#include +#include "Renderer.h" + class GuiComponent { public: - GuiComponent(); - ~GuiComponent(); - virtual void render() { }; - virtual unsigned int getLayer() { return 0; }; + void render(); + virtual void onRender() { }; + virtual unsigned int getLayer() { return BIT(0); }; + + void addChild(GuiComponent* comp); + void removeChild(GuiComponent* comp); + unsigned int getChildCount() { return mChildren.size(); } + GuiComponent* getChild(unsigned int i) { return mChildren.at(i); } +private: + std::vector mChildren; }; #endif diff --git a/src/Renderer.cpp b/src/Renderer.cpp index fff78d44d..30f666d93 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -1,4 +1,5 @@ #include "Renderer.h" +#include "GuiComponent.h" std::vector renderVector; @@ -31,3 +32,4 @@ void Renderer::render() } } } + diff --git a/src/Renderer.h b/src/Renderer.h index ffa61cee9..2d5ae1cab 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -5,14 +5,22 @@ #define BIT(x) (1 << (x)) -#include "GuiComponent.h" #include +#include + +class GuiComponent; + namespace Renderer { void registerComponent(GuiComponent* comp); void unregisterComponent(GuiComponent* comp); void render(); + + extern SDL_Surface* screen; + + //drawing commands + void drawRect(int x, int y, int w, int h, int color); } #endif diff --git a/src/Renderer_draw.cpp b/src/Renderer_draw.cpp new file mode 100644 index 000000000..63bf6a538 --- /dev/null +++ b/src/Renderer_draw.cpp @@ -0,0 +1,10 @@ +#include "Renderer.h" +#include + +SDL_Surface* Renderer::screen; + +void Renderer::drawRect(int x, int y, int h, int w, int color) +{ + SDL_Rect rect = {x, y, h, w}; + SDL_FillRect(Renderer::screen, &rect, color); +} diff --git a/src/components/GuiTitleScreen.cpp b/src/components/GuiTitleScreen.cpp new file mode 100644 index 000000000..8218ee9f4 --- /dev/null +++ b/src/components/GuiTitleScreen.cpp @@ -0,0 +1,16 @@ +#include "GuiTitleScreen.h" +#include "../Renderer.h" +#include + +GuiTitleScreen::GuiTitleScreen() +{ + Renderer::registerComponent(this); + + //add children here +} + +void GuiTitleScreen::onRender() +{ + Renderer::drawRect(0, 0, 640, 480, 0xFFFFFF); + std::cout << "rendering guititlescreen\n"; +} diff --git a/src/components/GuiTitleScreen.h b/src/components/GuiTitleScreen.h new file mode 100644 index 000000000..9f5b93409 --- /dev/null +++ b/src/components/GuiTitleScreen.h @@ -0,0 +1,13 @@ +#ifndef _GUITITLESCREEN_H_ +#define _GUITITLESCREEN_H_ + +#include "../GuiComponent.h" + +class GuiTitleScreen : public GuiComponent +{ +public: + GuiTitleScreen(); + void onRender(); +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 7006348f8..bb4a0f830 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,49 @@ #include #include #include "Renderer.h" - +#include "components/GuiTitleScreen.h" int main() { - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0) + //if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) + if(SDL_Init(SDL_INIT_EVERYTHING) != 0) { std::cerr << "Error - could not initialize SDL!\n"; return 1; } - + Renderer::screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE); + if(Renderer::screen == NULL) + { + std::cerr << "Error - could not set video mode!\n"; + return 1; + } + + + GuiTitleScreen* testGui = new GuiTitleScreen(); + + bool running = true; + while(running) + { + SDL_Event event; + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_KEYDOWN: + running = false; + break; + case SDL_QUIT: + running = false; + break; + } + } + + Renderer::render(); + SDL_Flip(Renderer::screen); + } + + delete testGui; SDL_Quit(); return 0;