Skip to content

Commit

Permalink
Added TTF and some basic screen output
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieCraig committed Oct 11, 2015
1 parent 5e40ceb commit ed13363
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 19 deletions.
Binary file added data/fonts/FreeSans.ttf
Binary file not shown.
5 changes: 2 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
CC=g++
CFLAGS=-ggdb -std=gnu++11 -MMD -MP -I/usr/include/SDL2 -Iinclude
LDFLAGS=-lSDL2 -lSDL2_image
CFLAGS=-ggdb -std=gnu++11 -MMD -MP -I/usr/include/SDL2 -Iinclude
LDFLAGS=-lSDL2 -lSDL2_image -lSDL2_ttf
OBJS=main.o gui.o logparser.o module.o gamedata.o can.o canframe.o
DEP := $(OBJS:.o=.d)

all: udsim
-include $(DEP)
-include $(DEP)

main.o:
$(CC) $(CFLAGS) -c main.cc
Expand Down
1 change: 0 additions & 1 deletion src/gamedata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ Module *GameData::isPossibleISOTP(canfd_frame *cf) {
void GameData::processLearned() {
if(verbose) cout << "Identified " << possible_modules.size() << " possible modules" << endl;
for(vector<Module>::iterator it = possible_modules.begin(); it != possible_modules.end(); ++it) {
cout << "DEBUG: ID: " << hex << it->getArbId() << " confidence " << it->confidence() << endl;
if(it->confidence() > CONFIDENCE_THRESHOLD) {
if(verbose) cout << "ID: " << it->getArbId() << " Looks like a UDS compliant module" << endl;
modules.push_back(*it);
Expand Down
116 changes: 106 additions & 10 deletions src/gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Gui::Gui() {
data_path = "../data/";
font_path = "../data/fonts/";
srand(time(NULL));
}

Expand All @@ -13,6 +14,7 @@ Gui::~Gui() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
TTF_Quit();
SDL_Quit();
}

Expand All @@ -21,6 +23,10 @@ int Gui::Init() {
printf("SDL Could not initializes\n");
return(-1);
}
if(TTF_Init() < 0) {
printf("TTF Could not be initialized\n");
return -2;
}
window = SDL_CreateWindow("UDSim", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Gui::getWidth(), Gui::getHeight(), SDL_WINDOW_SHOWN); // | SDL_WINDOW_RESIZABLE);
if(window == NULL) {
printf("Window could not be shown\n");
Expand All @@ -30,6 +36,12 @@ int Gui::Init() {
module_image = Gui::load_image("module.png");
base_texture = SDL_CreateTextureFromSurface(renderer, base_image);
module_texture = SDL_CreateTextureFromSurface(renderer, module_image);
module_ttf = Gui::load_font("FreeSans.ttf", 10);
if(!module_ttf) {
cout << TTF_GetError() << endl;
return -3;
}
log_ttf = Gui::load_font("FreeSans.ttf", 12);
Gui::Redraw();
return 0;
}
Expand All @@ -38,47 +50,86 @@ SDL_Surface *Gui::load_image(string imgname) {
return IMG_Load((Gui::getData() + imgname).c_str());
}

TTF_Font *Gui::load_font(string fontname, int size) {
SDL_RWops *file = SDL_RWFromFile((Gui::getFontPath() + fontname).c_str(), "rb");
if(!file) {
cout << "ERROR: " << SDL_GetError();
}
return TTF_OpenFontRW(file, 1, size);
}

void Gui::DrawModules() {
SDL_Rect update, pos;
SDL_Rect update, pos, car, trect;
SDL_Color font_color = { 255, 255, 255, 255 };
SDL_Surface *font = NULL;
char id_buf[16];
int state;
Module *selected = NULL;
vector <Module *>modules = gd.get_active_modules();
update.x = 0;
update.y = 0;
update.w = MODULE_W;
update.h = MODULE_H;
car.x = CAR_REGION_X;
car.y = CAR_REGION_Y;
car.w = CAR_REGION_W;
car.h = CAR_REGION_H;
SDL_RenderCopy(renderer, base_texture, &car, &car);
for(vector<Module *>::iterator it = modules.begin(); it != modules.end(); ++it) {
Module *mod = *it;
if(mod->getX() == 0 && mod->getY() == 0) { // Initialize location
mod->setX(260 + (rand() % 100));
mod->setY(80 + rand() % 100);
}
update.x = mod->getX();
update.y = mod->getY();
update.w = MODULE_W;
update.h = MODULE_H;
SDL_RenderCopy(renderer, base_texture, &update, &update);
memcpy(&pos, &update, sizeof(SDL_Rect));
update.x = 0;
update.y = 0;
pos.x = mod->getX();
pos.y = mod->getY();
pos.w = MODULE_W;
pos.h = MODULE_H;
if(mod->getIdTexture() == NULL) {
memset(id_buf, 0, 16);
snprintf(id_buf, 15, "%02X", mod->getArbId());
font = TTF_RenderText_Blended(module_ttf, id_buf, font_color);
mod->setIdTexture(SDL_CreateTextureFromSurface(renderer, font));
SDL_FreeSurface(font);
}
state = mod->getState();
switch(state) {
case STATE_IDLE:
update.x = 0;
SDL_RenderCopy(renderer, module_texture, &update, &pos);
SDL_RenderCopy(renderer, mod->getIdTexture(), NULL, &pos);
break;
case STATE_ACTIVE:
update.x = MODULE_W;
SDL_RenderCopy(renderer, module_texture, &update, &pos);
SDL_RenderCopy(renderer, mod->getIdTexture(), NULL, &pos);
break;
case STATE_MOUSEOVER:
update.x = MODULE_W * 2;
SDL_RenderCopy(renderer, module_texture, &update, &pos);
SDL_RenderCopy(renderer, mod->getIdTexture(), NULL, &pos);
break;
case STATE_SELECTED:
update.x = MODULE_W * 3;
/* Draw selected last in case dragged over another module */
selected = mod;
break;
default:
break;
}
}
if(selected) {
update.x = MODULE_W * 3;
pos.x = selected->getX();
pos.y = selected->getY();
SDL_RenderCopy(renderer, module_texture, &update, &pos);
SDL_RenderCopy(renderer, selected->getIdTexture(), NULL, &pos);
}
}

void Gui::Redraw() {
SDL_RenderCopy(renderer, base_texture, NULL, NULL);
Gui::DrawModules();
Gui::DrawLog();
SDL_RenderPresent(renderer);
}

Expand All @@ -94,6 +145,10 @@ void Gui::HandleMouseMotions(SDL_MouseMotionEvent motion) {
if(mod->getState() == STATE_SELECTED) {
mod->setX(mod->getX() + motion.xrel);
mod->setY(mod->getY() + motion.yrel);
if(mod->getX() < CAR_REGION_X) mod->setX(CAR_REGION_X);
if(mod->getY() < CAR_REGION_Y) mod->setY(CAR_REGION_Y);
if(mod->getX() > CAR_REGION_X + CAR_REGION_W - MODULE_W) mod->setX(CAR_REGION_X + CAR_REGION_W - MODULE_W);
if(mod->getY() > CAR_REGION_Y + CAR_REGION_H - MODULE_H) mod->setY(CAR_REGION_Y + CAR_REGION_H - MODULE_H);
change = true;
} else if(mod->getState() != STATE_MOUSEOVER) {
mod->setState(STATE_MOUSEOVER);
Expand Down Expand Up @@ -178,6 +233,47 @@ int Gui::HandleEvents() {
return running;
}

void Gui::DrawLog() {
SDL_Rect logrect, trect, pos;
SDL_Color font_color = { 255, 255, 255, 255 };
SDL_Surface *font;
SDL_Texture *font_texture; // TODO chang the string array to a cached texture array
int i;
logrect.x = LOG_REGION_X;
logrect.y = LOG_REGION_Y;
logrect.w = LOG_REGION_W;
logrect.h = LOG_REGION_H;
SDL_RenderCopy(renderer, base_texture, &logrect, &logrect);
for(i = 0; i < MAX_LOG_ENTRIES; i++) {
if(logbuff[i].size() > 0) {
font = TTF_RenderText_Blended(log_ttf, logbuff[i].c_str(), font_color);
font_texture = SDL_CreateTextureFromSurface(renderer, font);
trect.x = 0;
trect.y = 0;
trect.h = font->h;
if(font->w > LOG_REGION_W) {
trect.w = LOG_REGION_W;
} else {
trect.w = font->w;
}
SDL_FreeSurface(font);
pos.x = LOG_REGION_X;
pos.y = LOG_REGION_Y + LOG_REGION_H - (trect.h * i);
pos.h = trect.h;
pos.w = trect.w;
SDL_RenderCopy(renderer, font_texture, &trect, &pos);
}
}
}

void Gui::Msg(string m) {
int i;
if(verbose) { cout << m << endl; }
for(i = MAX_LOG_ENTRIES-1; i > 0; i--) {
logbuff[i] = logbuff[i-1];
}
logbuff[0] = m;
Gui::DrawLog();
SDL_RenderPresent(renderer);
}

15 changes: 15 additions & 0 deletions src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>

#include "gamedata.h"
#include "module.h"

using namespace std;

#define MAX_LOG_ENTRIES 23
// The region of the vehicle on the screen
#define CAR_REGION_X 245
#define CAR_REGION_Y 58
#define CAR_REGION_H 327
#define CAR_REGION_W 165
// Log window region
#define LOG_REGION_X 450
#define LOG_REGION_Y 40
#define LOG_REGION_H 446
#define LOG_REGION_W 270

extern GameData gd;

Expand All @@ -31,21 +38,27 @@ class Gui {
int Init();
void setVerbose(int v) { verbose = v; }
void setData(string s) { data_path = s; }
void setFontPath(string f) { font_path = f; }
string getData() { return data_path; }
string getFontPath() { return font_path; }
SDL_Surface *load_image(string);
TTF_Font *load_font(string, int);
void Redraw();
int HandleEvents();
void Msg(string);

private:
void DrawModules();
void DrawLog();
void HandleMouseMotions(SDL_MouseMotionEvent);
void HandleMouseClick(SDL_MouseButtonEvent);
bool isOverCarRegion(int, int);
int width = 720;
int height = 486;
int verbose = 0;
string data_path;
string font_path;
string logbuff[MAX_LOG_ENTRIES];
SDL_Window *window = NULL;
SDL_Surface *screen = NULL;
SDL_Renderer *renderer = NULL;
Expand All @@ -54,6 +67,8 @@ class Gui {
SDL_Surface *module_image = NULL;
SDL_Texture *module_texture = NULL;
SDL_Event event;
TTF_Font *module_ttf = NULL;
TTF_Font *log_ttf = NULL;
};

#endif
5 changes: 3 additions & 2 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void Usage(string msg) {
}

int main(int argc, char *argv[]) {
int running = 1, opt;
int running = 1, opt, res;
int verbose = 0;
bool process_log = false;
Gui gui;
Expand Down Expand Up @@ -52,7 +52,8 @@ int main(int argc, char *argv[]) {
gd.setCan(new Can(argv[optind]));

gui.setVerbose(verbose);
gui.Init();
res=gui.Init();
if(res < 0) exit(3);

gd.setVerbose(verbose);
while(running) {
Expand Down
8 changes: 5 additions & 3 deletions src/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <vector>
#include <stdlib.h>
#include <string.h>
//#include <net/if.h>
//#include <linux/can.h>
//#include <linux/can/raw.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "canframe.h"

Expand Down Expand Up @@ -45,6 +44,8 @@ class Module
int getY() { return _y; }
void setX(int x) { _x = x; }
void setY(int y) { _y = y; }
SDL_Texture *getIdTexture() { return id_texture; }
void setIdTexture(SDL_Texture *t) { id_texture = t; }
private:
int arbId;
int matched_isotp = 0;
Expand All @@ -55,6 +56,7 @@ class Module
int state = STATE_IDLE;
int _x = 0;
int _y = 0;
SDL_Texture *id_texture = NULL;
vector<CanFrame *>can_history;
Module *positive_responder = NULL;
Module *negative_responder = NULL;
Expand Down

0 comments on commit ed13363

Please sign in to comment.