Added InputManager; GuiComponents can register themselves to receive input events.

Added text rendering to Renderer, which uses SDL_ttf. Using LinLibertine_R.ttf font (GPL).
A lot more - soon I should have the "skeleton" done.
This commit is contained in:
Aloshi 2012-07-19 20:08:29 -05:00
parent 1177fde6c3
commit 42a39c52e6
15 changed files with 279 additions and 6 deletions

3
.gitignore vendored
View file

@ -10,3 +10,6 @@
*.lai *.lai
*.la *.la
*.a *.a
#Compiled executable
emulationstation

BIN
LinLibertine_R.ttf Normal file

Binary file not shown.

View file

@ -1,7 +1,7 @@
CC=g++ CC=g++
CFLAGS=-c -Wall CFLAGS=-c -Wall
LDFLAGS=-lSDL LDFLAGS=-lSDL -lSDL_ttf
SRCSOURCES=main.cpp Renderer.cpp Renderer_draw.cpp GuiComponent.cpp components/GuiTitleScreen.cpp SRCSOURCES=main.cpp Renderer.cpp Renderer_draw.cpp GuiComponent.cpp InputManager.cpp components/GuiTitleScreen.cpp components/GuiList.cpp components/GuiGameList.cpp
SOURCES=$(addprefix src/,$(SRCSOURCES)) SOURCES=$(addprefix src/,$(SRCSOURCES))
OBJECTS=$(SOURCES:.cpp=.o) OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=emulationstation EXECUTABLE=emulationstation

View file

@ -3,12 +3,14 @@
#include <vector> #include <vector>
#include "Renderer.h" #include "Renderer.h"
#include "InputManager.h"
class GuiComponent class GuiComponent
{ {
public: public:
void render(); void render();
virtual void onRender() { }; virtual void onRender() { };
virtual void onInput(InputManager::InputButton button, bool keyDown) { };
virtual unsigned int getLayer() { return BIT(0); }; virtual unsigned int getLayer() { return BIT(0); };
void addChild(GuiComponent* comp); void addChild(GuiComponent* comp);

73
src/InputManager.cpp Normal file
View file

@ -0,0 +1,73 @@
#include "InputManager.h"
#include "GuiComponent.h"
#include <iostream>
std::vector<GuiComponent*> InputManager::inputVector;
void InputManager::registerComponent(GuiComponent* comp)
{
inputVector.push_back(comp);
}
void InputManager::unregisterComponent(GuiComponent* comp)
{
for(unsigned int i = 0; i < inputVector.size(); i++)
{
if(inputVector.at(i) == comp)
{
inputVector.erase(inputVector.begin() + i);
break;
}
}
}
void InputManager::processEvent(SDL_Event* event)
{
bool keyDown = false;
if(event->key.state == SDL_PRESSED)
keyDown = true;
std::cout << "Processing keyboard event\n";
//get InputButton from the event
InputButton button;
switch(event->key.keysym.sym)
{
case SDLK_LEFT:
button = LEFT;
break;
case SDLK_RIGHT:
button = RIGHT;
break;
case SDLK_UP:
button = UP;
break;
case SDLK_DOWN:
button = DOWN;
break;
case SDLK_SPACE:
button = BUTTON1;
break;
//so the compiler doesn't complain
default:
break;
}
//catch emergency quit event
if(event->key.keysym.sym == SDLK_F4)
{
SDL_Event* quit = new SDL_Event();
quit->type = SDL_QUIT;
SDL_PushEvent(quit);
std::cout << "Pushing quit event\n";
}
for(unsigned int i = 0; i < inputVector.size(); i++)
{
inputVector.at(i)->onInput(button, keyDown);
}
}

23
src/InputManager.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef _INPUTMANAGER_H_
#define _INPUTMANAGER_H_
#include <vector>
#include <SDL/SDL.h>
class GuiComponent;
namespace InputManager {
void registerComponent(GuiComponent* comp);
void unregisterComponent(GuiComponent* comp);
//enum for identifying input type, regardless of configuration
enum InputButton { UP, DOWN, LEFT, RIGHT, BUTTON1, BUTTON2};
void processEvent(SDL_Event* event);
extern std::vector<GuiComponent*> inputVector;
}
#endif

View file

@ -3,6 +3,9 @@
std::vector<GuiComponent*> renderVector; std::vector<GuiComponent*> renderVector;
unsigned int Renderer::getScreenWidth() { return 640; }
unsigned int Renderer::getScreenHeight() { return 480; }
void Renderer::registerComponent(GuiComponent* comp) void Renderer::registerComponent(GuiComponent* comp)
{ {
renderVector.push_back(comp); renderVector.push_back(comp);

View file

@ -7,6 +7,8 @@
#include <vector> #include <vector>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <string>
class GuiComponent; class GuiComponent;
@ -18,9 +20,17 @@ namespace Renderer
void render(); void render();
extern SDL_Surface* screen; extern SDL_Surface* screen;
extern TTF_Font* font;
unsigned int getScreenWidth();
unsigned int getScreenHeight();
//drawing commands //drawing commands
void drawRect(int x, int y, int w, int h, int color); void drawRect(int x, int y, int w, int h, int color);
void drawText(std::string text, int x, int y, SDL_Color& color);
void drawCenteredText(std::string text, int y, SDL_Color& color);
void loadFonts();
} }
#endif #endif

View file

@ -1,10 +1,59 @@
#include "Renderer.h" #include "Renderer.h"
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <iostream>
SDL_Surface* Renderer::screen; SDL_Surface* Renderer::screen;
TTF_Font* Renderer::font;
void Renderer::drawRect(int x, int y, int h, int w, int color) void Renderer::drawRect(int x, int y, int h, int w, int color)
{ {
SDL_Rect rect = {x, y, h, w}; SDL_Rect rect = {x, y, h, w};
SDL_FillRect(Renderer::screen, &rect, color); SDL_FillRect(Renderer::screen, &rect, color);
} }
void Renderer::loadFonts()
{
font = TTF_OpenFont("LinLibertine_R.ttf", 72);
if(!font)
{
std::cerr << "Error - could not load font!\n";
std::cerr << TTF_GetError() << "\n";
return;
}
}
void Renderer::drawText(std::string text, int x, int y, SDL_Color& color)
{
if(!font)
loadFonts();
//color = {255, 0, 0};
SDL_Surface* textSurf = TTF_RenderText_Blended(font, text.c_str(), color);
if(textSurf == NULL)
{
std::cerr << "Error - could not render text \"" << text << "\" to surface!\n";
std::cerr << TTF_GetError() << "\n";
return;
}
SDL_Rect dest = {x, y};
SDL_BlitSurface(textSurf, NULL, screen, &dest);
SDL_FreeSurface(textSurf);
}
void Renderer::drawCenteredText(std::string text, int y, SDL_Color& color)
{
if(!font)
loadFonts();
int w, h;
TTF_SizeText(font, text.c_str(), &w, &h);
int x = (int)getScreenWidth() - w;
x *= 0.5;
drawText(text, x, y, color);
}

View file

@ -0,0 +1,16 @@
#include "GuiGameList.h"
GuiGameList::GuiGameList(std::string systemName)
{
mSystemName = systemName;
mList = new GuiList();
addChild(mList);
}
void GuiGameList::onRender()
{
SDL_Color color = {0, 0, 255};
Renderer::drawCenteredText(mSystemName, 2, color);
}

View file

@ -0,0 +1,19 @@
#ifndef _GUIGAMELIST_H_
#define _GUIGAMELIST_H_
#include "../GuiComponent.h"
#include "GuiList.h"
#include <string>
class GuiGameList : GuiComponent
{
public:
GuiGameList(std::string systemName);
void onRender();
private:
std::string mSystemName;
GuiList* mList;
};
#endif

View file

@ -0,0 +1,33 @@
#include "GuiList.h"
GuiList::GuiList()
{
mSelection = 0;
}
void GuiList::onRender()
{
}
void GuiList::addObject(std::string name, void* obj)
{
mNameVector.push_back(name);
mPointerVector.push_back(obj);
}
void GuiList::clearObjects()
{
mNameVector.clear();
mPointerVector.clear();
}
std::string GuiList::getSelectedName()
{
return mNameVector.at(mSelection);
}
void* GuiList::getSelectedObject()
{
return mPointerVector.at(mSelection);
}

27
src/components/GuiList.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef _GUILIST_H_
#define _GUILIST_H_
#include "../Renderer.h"
#include "../GuiComponent.h"
#include <vector>
#include <string>
class GuiList : public GuiComponent
{
public:
GuiList();
void onRender();
void addObject(std::string name, void* obj);
void clearObjects();
std::string getSelectedName();
void* getSelectedObject();
private:
std::vector<std::string> mNameVector;
std::vector<void*> mPointerVector;
unsigned int mSelection;
};
#endif

View file

@ -12,5 +12,6 @@ GuiTitleScreen::GuiTitleScreen()
void GuiTitleScreen::onRender() void GuiTitleScreen::onRender()
{ {
Renderer::drawRect(0, 0, 640, 480, 0xFFFFFF); Renderer::drawRect(0, 0, 640, 480, 0xFFFFFF);
std::cout << "rendering guititlescreen\n"; SDL_Color color = {255, 0, 0};
Renderer::drawCenteredText("EmulationStation", 5, color);
} }

View file

@ -1,16 +1,21 @@
#include <iostream> #include <iostream>
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "Renderer.h" #include "Renderer.h"
#include "components/GuiTitleScreen.h" #include "components/GuiTitleScreen.h"
int main() int main()
{ {
//if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{ {
std::cerr << "Error - could not initialize SDL!\n"; std::cerr << "Error - could not initialize SDL!\n";
return 1; return 1;
} }
if(TTF_Init() != 0)
{
std::cerr << "Error - could not initialize SDL_ttf!\n";
return 1;
}
Renderer::screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE); Renderer::screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
if(Renderer::screen == NULL) if(Renderer::screen == NULL)
@ -19,9 +24,11 @@ int main()
return 1; return 1;
} }
SDL_ShowCursor(false);
GuiTitleScreen* testGui = new GuiTitleScreen(); GuiTitleScreen* testGui = new GuiTitleScreen();
bool running = true; bool running = true;
while(running) while(running)
{ {
@ -31,8 +38,12 @@ int main()
switch(event.type) switch(event.type)
{ {
case SDL_KEYDOWN: case SDL_KEYDOWN:
running = false; InputManager::processEvent(&event);
break; break;
case SDL_KEYUP:
InputManager::processEvent(&event);
break;
case SDL_QUIT: case SDL_QUIT:
running = false; running = false;
break; break;
@ -45,6 +56,9 @@ int main()
delete testGui; delete testGui;
std::cout << "EmulationStation cleanly shutting down...\n";
TTF_Quit();
SDL_Quit(); SDL_Quit();
return 0; return 0;
} }