mirror of
https://github.com/libretro/libretro-chailove.git
synced 2024-11-30 03:30:26 +00:00
Add ImageData and Images
This commit is contained in:
parent
2b1f5f9855
commit
af83a7a4f7
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
|
13
Makefile
13
Makefile
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
32
chaigame/image.cpp
Normal 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
15
chaigame/image.h
Normal 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
|
@ -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
4
chaigame/src/Image.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include "Image.h"
|
||||
|
||||
namespace chaigame {
|
||||
}
|
13
chaigame/src/Image.h
Normal file
13
chaigame/src/Image.h
Normal 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
|
41
chaigame/src/ImageData.cpp
Normal file
41
chaigame/src/ImageData.cpp
Normal 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
33
chaigame/src/ImageData.h
Normal 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
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user