From 67beaa4ea52e431cfcfc6a366c5544dbe5bec54b Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Sat, 19 Dec 2009 09:00:29 +0000 Subject: [PATCH] Add game info class and structure --- Makefile | 7 ++-- game_info.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ game_info.hh | 37 +++++++++++++++++++ 3 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 game_info.cpp create mode 100644 game_info.hh diff --git a/Makefile b/Makefile index 3ac2044..1cfa84f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ -OBJS=menu.oo main.oo utils.oo gui.oo dialogue_box.oo menu_messages.oo timer.oo +OBJS=menu.oo main.oo utils.oo gui.oo dialogue_box.oo menu_messages.oo \ + timer.oo game_info.oo all: menu @@ -7,10 +8,12 @@ all: menu menu.oo: menu.cpp menu.hh utils.hh font.hh Makefile -gui.oo: Makefile gui.cpp gui.hh font.hh menu.hh sdl_ttf_font.hh \ +gui.oo: gui.cpp gui.hh Makefile font.hh menu.hh sdl_ttf_font.hh \ dialogue_box.hh help_box.hh main_menu.cpp disc_menu.cpp \ file_browser.hh timer.hh +game_info.oo: game_info.hh game_info.cpp + utils.oo: utils.cpp utils.hh Makefile timer.oo: timer.cpp timer.hh utils.hh Makefile diff --git a/game_info.cpp b/game_info.cpp new file mode 100644 index 0000000..07bcd3c --- /dev/null +++ b/game_info.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + +#include "game_info.hh" +#include "utils.hh" + +GameInfo::GameInfo() +{ + this->name = NULL; + this->author = NULL; + this->screenshot = NULL; +} + +GameInfo::~GameInfo() +{ + this->resetDefaults(); +} + +void GameInfo::resetDefaults() +{ + free(this->name); + free(this->author); + SDL_FreeSurface(this->screenshot); + + this->name = NULL; + this->author = NULL; + this->screenshot = NULL; +} + +struct game_info *GameInfo::dump(size_t *out_sz) +{ + size_t total_sz = sizeof(struct game_info); + size_t png_sz; + struct game_info *out; + void *png_data; + + if (this->screenshot) + return NULL; + + /* Convert surface to PNG */ + png_data = sdl_surface_to_png(this->screenshot, &png_sz); + panic_if(!png_data, "Cannot create PNG from surface\n"); + + total_sz += strlen(this->author) + 1; + total_sz += strlen(this->name) + 1; + + total_sz += png_sz; + /* 4-byte align */ + total_sz += 4 - (total_sz & 3); + + /* Allocate everything */ + out = (struct game_info*)xmalloc(total_sz); + out->sz = total_sz; + out->dummy = 0; + out->author_off = 0; /* Starts AFTER the header */ + out->name_off = out->author_off + strlen(this->author) + 1; + out->screenshot_off = out->name_off + strlen(this->name) + 1; + + memcpy(out->data + out->author_off, this->author, strlen(this->author) + 1); + memcpy(out->data + out->name_off, this->name, strlen(this->name) + 1); + memcpy(out->data + out->screenshot_off, png_data, png_sz); + + out->sz = htonl(out->sz); + out->author_off = htons(out->author_off); + out->name_off = htons(out->name_off); + out->screenshot_off = htons(out->screenshot_off); + + return out; +} + +void GameInfo::fromDump(struct game_info *gi, size_t sz) +{ + SDL_RWops *rw; + + gi->sz = ntohl(gi->sz); + gi->author_off = ntohs(gi->author_off); + gi->name_off = ntohs(gi->name_off); + gi->screenshot_off = ntohs(gi->screenshot_off); + + this->author = xstrdup((char*)gi->data + gi->author_off); + this->name = xstrdup((char*)gi->data + gi->name_off); + + rw = SDL_RWFromMem(gi->data + gi->screenshot_off, + gi->sz - gi->screenshot_off); + if (!rw) + goto bail_out; + + this->screenshot = IMG_Load_RW(rw, 0); + SDL_FreeRW(rw); + if (!this->screenshot) + goto bail_out; + + return; + +bail_out: + this->resetDefaults(); +} diff --git a/game_info.hh b/game_info.hh new file mode 100644 index 0000000..ff8c3a2 --- /dev/null +++ b/game_info.hh @@ -0,0 +1,37 @@ +#ifndef __GAME_INFO_HH__ +#define __GAME_INFO_HH__ + +#include + +struct game_info +{ + uint32_t sz; + uint16_t author_off; + uint16_t name_off; + uint16_t screenshot_off; /* In PNG format */ + uint16_t dummy; + uint8_t data[]; /* 4-byte aligned */ +}; + +class GameInfo +{ +public: + GameInfo(); + + ~GameInfo(); + + void resetDefaults(); + + /** Returns an allocated dump structure */ + struct game_info *dump(size_t *out_sz); + + /** Fill in this game info object from a structure */ + void fromDump(struct game_info *data, size_t sz); + +protected: + char *name; + char *author; + SDL_Surface *screenshot; +}; + +#endif /*__GAME_INFO_HH__ */