Add ImageData and Images

This commit is contained in:
Rob Loach 2017-06-27 15:50:57 -04:00
parent 2b1f5f9855
commit af83a7a4f7
No known key found for this signature in database
GPG Key ID: 627C60834A74A21A
18 changed files with 214 additions and 7 deletions

View File

@ -27,6 +27,7 @@ Application* Application::getInstance() {
void Application::quit(void) {
filesystem.unload();
image.unload();
// Tell SDL to quit.
SDL_Quit();
}
@ -58,6 +59,8 @@ bool Application::load(std::string file = "") {
// Initalize the chaigame subsystems.
keyboard.load();
image.load();
// Initialize the file system.
filesystem.load(file);
@ -136,6 +139,11 @@ void Application::draw(){
}
graphics.rectangle(x, y, 100, 100, 0, 255, 255, 255);
//static chaigame::Image* pic = graphics.newImage("logo.png");
//graphics.draw(pic);
// Render the game.
script->draw();

View File

@ -13,8 +13,9 @@ public:
chaigame::keyboard keyboard;
chaigame::script* script;
chaigame::graphics graphics;
chaigame::filesystem filesystem;
chaigame::graphics graphics;
chaigame::image image;
void quit(void);
bool load(std::string);

View File

@ -62,7 +62,11 @@ OBJECTS := libretro.o Application.o \
chaigame/graphics.o \
chaigame/keyboard.o \
chaigame/script.o \
chaigame/filesystem.o
chaigame/filesystem.o \
chaigame/image.o \
chaigame/src/ImageData.o \
chaigame/src/Image.o \
vendor/physfs/extras/physfsrwops.o
all: vendor/physfs/libphysfs.a vendor/libretro-common/include/libretro.h $(TARGET)
@ -73,10 +77,11 @@ else
endif
LDFLAGS += $(fpic) $(SHARED) \
vendor/sdl-libretro/libSDL_gfx_$(SDL_PREFIX).a \
vendor/sdl-libretro/libSDL_$(SDL_PREFIX).a \
vendor/sdl-libretro/libSDL_gfx_$(SDL_PREFIX).a \
vendor/sdl-libretro/SDL_image_$(SDL_PREFIX).a \
vendor/physfs/libphysfs.a \
-ldl \
-ldl -ljpeg -lpng \
-lpthread $(EXTRA_LDF)
FLAGS += -I. \
-Ivendor/sdl-libretro/include \
@ -105,8 +110,6 @@ $(TARGET): $(OBJECTS)
clean:
rm -f $(TARGET) $(OBJECTS)
#rm -rf vendor
#git submodule update
vendor/libretro-common/include/libretro.h:
git submodule init

View File

@ -5,5 +5,8 @@
#include "keyboard.h"
#include "script.h"
#include "filesystem.h"
#include "image.h"
#include "src/ImageData.h"
#include "src/Image.h"
#endif

View File

@ -4,6 +4,7 @@
#include "filesystem.h"
#include <string>
#include "../Application.h"
#include "vendor/physfs/extras/physfsrwops.h"
namespace chaigame {
bool filesystem::load() {
@ -39,4 +40,13 @@ namespace chaigame {
SDL_FreeRW(rw);
*/
SDL_RWops* filesystem::openRW(std::string filename) {
SDL_RWops* rw = PHYSFSRWOPS_openRead(filename.c_str());
if (rw != NULL) {
return rw;
}
// TODO: Add error reporting.
return NULL;
}
}

View File

@ -2,6 +2,7 @@
#define FILESYSTEM_H
#include <string>
#include <SDL.h>
#include <physfs.h>
namespace chaigame {
@ -10,6 +11,7 @@ namespace chaigame {
bool load(std::string file);
bool load();
bool unload();
SDL_RWops* openRW(std::string filename);
};
}

View File

@ -4,9 +4,32 @@
#include "graphics.h"
#include "../Application.h"
#include "src/ImageData.h"
#include "src/Image.h"
namespace chaigame {
void graphics::rectangle(Sint16 x, Sint16 y, Sint16 width, Sint16 height, Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
rectangleRGBA(Application::getInstance()->screen, x, y, x + width, y + height, r, g, b, a);
}
void graphics::draw(ImageData* image, int x, int y) {
if (image && image->loaded()) {
SDL_BlitSurface(image->surface, NULL, Application::getInstance()->screen, NULL);
}
}
void graphics::draw(Image* image) {
if (image && image->loaded()) {
SDL_BlitSurface(image->surface, NULL, Application::getInstance()->screen, NULL);
}
}
Image* graphics::newImage(std::string filename) {
Image* image = new Image(filename);
if (image->loaded()) {
return image;
}
return NULL;
}
}

View File

@ -4,10 +4,17 @@
#include <SDL.h>
#include <SDL_gfxPrimitives.h>
#include "src/ImageData.h"
#include "src/Image.h"
namespace chaigame {
class graphics {
public:
void rectangle(Sint16 x, Sint16 y, Sint16 width, Sint16 height, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
void draw(ImageData* data, int x, int y);
void draw(Image* data);
Image* newImage(std::string filename);
};
}

32
chaigame/image.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "image.h"
#include "../Application.h"
#include <SDL.h>
#include <string>
#include <SDL_image.h>
#include "src/ImageData.h"
namespace chaigame {
chaigame::ImageData* image::newImageData(std::string filename) {
ImageData* image = new chaigame::ImageData(filename);
if (image->loaded()) {
return image;
}
return NULL;
}
bool image::load() {
int flags = IMG_INIT_PNG | IMG_INIT_JPG;
int initted = IMG_Init(flags);
if(flags != (initted & flags)) {
printf("IMG_Init: Failed to init required jpg and png support!\n");
printf("IMG_Init: %s\n", IMG_GetError());
return false;
}
return true;
}
bool image::unload() {
IMG_Quit();
return true;
}
}

15
chaigame/image.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef _IMAGE_H_INCLUDED_
#define _IMAGE_H_INCLUDED_
#include "src/ImageData.h"
namespace chaigame {
class image {
public:
bool load();
bool unload();
chaigame::ImageData* newImageData(std::string filename);
};
}
#endif

View File

@ -8,6 +8,9 @@ namespace chaigame {
#ifndef __DISABLE_CHAISCRIPT__
// Register the Graphics module.
chai.add(chaiscript::fun(&chaigame::graphics::rectangle), "rectangle");
chai.add(chaiscript::fun(&chaigame::graphics::newImage), "newImage");
chai.add(chaiscript::fun<void, graphics, Image*>(&chaigame::graphics::draw), "draw");
chai.add_global(chaiscript::var(std::ref(Application::getInstance()->graphics)), "graphics");
// Register the Keyboard module.
@ -15,6 +18,10 @@ namespace chaigame {
chai.add(chaiscript::fun(&chaigame::keyboard::isDown), "isDown");
chai.add_global(chaiscript::var(std::ref(Application::getInstance()->keyboard)), "keyboard");
// Register the Image module.
chai.add(chaiscript::fun(&chaigame::image::newImageData), "newImageData");
chai.add_global(chaiscript::var(std::ref(Application::getInstance()->image)), "image");
// Load main.chai.
chai.eval_file("main.chai");

4
chaigame/src/Image.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "Image.h"
namespace chaigame {
}

13
chaigame/src/Image.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _SRCIMAGE_H_INCLUDED_
#define _SRCIMAGE_H_INCLUDED_
#include <string>
#include "ImageData.h"
namespace chaigame {
class Image : public ImageData {
using ImageData::ImageData;
};
}
#endif

View File

@ -0,0 +1,41 @@
#include "ImageData.h"
#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include "../../Application.h"
namespace chaigame {
ImageData::ImageData(SDL_RWops* rw) {
loadFromRW(rw);
}
bool ImageData::loaded() {
return surface != NULL;
}
bool ImageData::loadFromRW(SDL_RWops* rw) {
surface = IMG_Load_RW(rw, 1);
if (!surface) {
printf("IMG_Load_RW: %s\n", IMG_GetError());
return false;
}
return true;
}
bool ImageData::destroy() {
printf("DESTROY IMAGE");
if (!surface) {
SDL_FreeSurface(surface);
surface = NULL;
}
return true;
}
ImageData::~ImageData() {
destroy();
}
ImageData::ImageData(std::string filename) {
SDL_RWops* image = Application::getInstance()->filesystem.openRW(filename);
loadFromRW(image);
}
}

33
chaigame/src/ImageData.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef _IMAGEDATA_H_INCLUDED_
#define _IMAGEDATA_H_INCLUDED_
#include <SDL.h>
#include <string>
namespace chaigame {
class ImageData {
public:
SDL_Surface* surface;
ImageData(SDL_RWops* rw);
ImageData(std::string filename);
~ImageData();
bool loaded();
bool loadFromRW(SDL_RWops* rw);
bool destroy();
/* TODO: Add rwops API
SDL_RWops* rw;
if ((rw=PHYSFSRWOPS_openRead(filepath.c_str()) == NULL)
{
return false; //file doesn't exist
}
SDL_Surface* surface = IMG_Load_RW(rw, 0);
SDL_FreeRW(rw);
*/
//void newImageData(std::string filename);
};
}
#endif

View File

@ -38,7 +38,6 @@ void libretro_audio_cb(int16_t left, int16_t right) {
short int libretro_input_state_cb(unsigned port, unsigned device, unsigned index, unsigned id) {
return input_state_cb(port,device,index,id);
}
#ifdef __cplusplus
}

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@ -3,10 +3,14 @@ print("Hello World, from Chai")
global x;
global y;
global logo;
def load() {
// Called when loading the game.
x = 10
y = 100
logo = graphics.newImage("logo.png")
}
def update(delta) {
@ -28,4 +32,6 @@ def update(delta) {
def draw() {
// Called to render the game state.
graphics.rectangle(x, y, 100, 20, 0, 0, 255, 255)
graphics.draw(logo)
}