void Sprite::addImage(const char*res, int state) { img = Iw2DCreateImage(res); }
#ifndef _RESOURCEMANAGER_H_ #define _RESOURCEMANAGER_H_ #include <map> #include <string> #include "s3e.h" #include "IwResManager.h" #include "IwSound.h" #include "ResourceHolder.h" using namespace std; class ResourceManager { private: map<string, ResourceHolder*> res; public: ResourceManager(): res() {} void init(); void release(); ResourceHolder* load(const char* name, int loc); typedef map<string, ResourceHolder*>::iterator RIter; typedef pair<string, ResourceHolder*> RPair; }; extern ResourceManager rm; #endif // _RESOURCEMANAGER_H_
#include "ResourceManager.h" #include "Locale.h" ResourceManager rm; void ResourceManager::init() { IwResManagerInit(); } void ResourceManager::release() { for (RIter p = res.begin(); p != res.end(); ++p) { delete p->second; } res.clear(); IwResManagerTerminate(); } ResourceHolder* ResourceManager::load(const char* name, int loc) { ResourceHolder* r = NULL; string nm(name); RIter p = res.find(nm); if (p == res.end()) { r = new ResourceHolder(name, loc); res.insert(RPair(nm, r)); } else { r = p->second; } return r; }
#ifndef _RESOURCEHOLDER_H_ #define _RESOURCEHOLDER_H_ #include <string> #include "s3e.h" #include "Iw2D.h" #include "IwResManager.h" using namespace std; class ResourceHolder { private: string name; int loc; CIw2DImage* data; public: ResourceHolder(const char* name, int loc); ~ResourceHolder() {unload();} void load(); void unload(); CIw2DImage* getData(); }; #endif // _RESOURCEHOLDER_H_ </spoiler> <spoiler title="ResourceHolder.cpp"> <source lang="cpp"> #include "ResourceHolder.h" #include "Locale.h" ResourceHolder::ResourceHolder(const char* name, int loc): name(name) , loc(loc) , data(NULL) { } void ResourceHolder::load() { if (data == NULL) { CIwResGroup* resGroup; const char* groupName = Locale::getGroupName(loc); if (groupName != NULL) { resGroup = IwGetResManager()->GetGroupNamed(groupName); IwGetResManager()->SetCurrentGroup(resGroup); data = Iw2DCreateImageResource(name.c_str()); } else { data = Iw2DCreateImage(name.c_str()); } } } void ResourceHolder::unload() { if (data != NULL) { delete data; data = NULL; } } CIw2DImage* ResourceHolder::getData() { load(); return data; }
CIwResGroup { name "locale_ru" "./locale_ru/play.png" "./locale_ru/setup.png" "./locale_ru/musicoff.png" "./locale_ru/musicon.png" "./locale_ru/soundoff.png" "./locale_ru/soundon.png" }
#ifndef _LOCALE_H_ #define _LOCALE_H_ enum ELocale { elNothing = 0x0, elImage = 0x1, elSound = 0x2, elEnImage = 0x5, elRuImage = 0x9, elEnSound = 0x6, elRuSound = 0xA }; class Locale { public: static int getCurrentImageLocale(); static int getCurrentSoundLocale(); static int getCommonImageLocale() {return elImage;} static int getCommonSoundLocale() {return elSound;} static const char* getGroupName(int locale); }; #endif // _LOCALE_H_
#include "Locale.h" #include "s3e.h" const char* Locale::getGroupName(int locale) { switch (locale) { case elImage: return "images"; case elEnSound: case elRuSound: case elSound: return "sounds"; case elEnImage: return "locale_en"; case elRuImage: return "locale_ru"; default: return NULL; } } int Locale::getCurrentImageLocale() { int32 lang = s3eDeviceGetInt(S3E_DEVICE_LANGUAGE); switch (lang) { case S3E_DEVICE_LANGUAGE_RUSSIAN: return elRuImage; default: return elEnImage; } } int Locale::getCurrentSoundLocale() { int32 lang = s3eDeviceGetInt(S3E_DEVICE_LANGUAGE); switch (lang) { case S3E_DEVICE_LANGUAGE_RUSSIAN: return elRuSound; default: return elEnSound; } }
[SOUND] MaxChannels=16
#!/usr/bin/env mkb options { module_path="$MARMALADE_ROOT/examples" } subprojects { iw2d iwresmanager SoundEngine } ... files { ... [Data] (data) locale_en.group locale_ru.group sounds.group } assets { (data) background.png sprite.png music.mp3 (data-ram/data-gles1, data) locale_en.group.bin locale_ru.group.bin sounds.group.bin }
CIwResGroup { name "sounds" "./sounds/menubutton.wav" "./sounds/sound.wav" CIwSoundSpec { name "menubutton" data "menubutton" vol 0.9 loop false } CIwSoundSpec { name "sound" data "sound" vol 0.9 loop false } CIwSoundGroup { name "sound_effects" maxPolyphony 8 killOldest true addSpec "menubutton" addSpec "sound" } }
void ResourceManager::init() { IwResManagerInit(); #ifdef IW_BUILD_RESOURCES IwGetResManager()->AddHandler(new CIwResHandlerWAV); #endif IwGetResManager()->LoadGroup("sounds.group"); if (Locale::getCurrentImageLocale() == elEnImage) { IwGetResManager()->LoadGroup("locale_en.group"); } if (Locale::getCurrentImageLocale() == elRuImage) { IwGetResManager()->LoadGroup("locale_ru.group"); } } void ResourceManager::release() { for (RIter p = res.begin(); p != res.end(); ++p) { delete p->second; } res.clear(); IwResManagerTerminate(); IwSoundTerminate(); }
#include "Main.h" #include "s3e.h" #include "Iw2D.h" #include "IwGx.h" #include "IwSound.h" #include "ResourceManager.h" #include "TouchPad.h" #include "Desktop.h" #include "Scene.h" #include "Background.h" #include "Sprite.h" void init() { // Initialise Mamrlade graphics system and Iw2D module IwGxInit(); Iw2DInit(); // Init IwSound IwSoundInit(); // Set the default background clear colour IwGxSetColClear(0x0, 0x0, 0x0, 0); // Initialise the resource manager rm.init(); touchPad.init(); desktop.init(); } void release() { desktop.release(); touchPad.release(); // Shut down the resource manager rm.release(); Iw2DTerminate(); IwGxTerminate(); } int main() { init(); { Scene scene; new Background(&scene, "background.png", 1, elNothing); new Sprite(&scene, "sprite.png", 122, 100, 2, elNothing); desktop.setScene(&scene); int32 duration = 1000 / 25; // Main Game Loop while (!desktop.isQuitMessageReceived()) { // Update keyboard system s3eKeyboardUpdate(); // Update Iw Sound Manager IwGetSoundManager()->Update(); // Update touchPad.update(); uint64 timestamp = s3eTimerGetMs(); desktop.update(timestamp); // Clear the screen IwGxClear(IW_GX_COLOUR_BUFFER_F | IW_GX_DEPTH_BUFFER_F); touchPad.clear(); // Refresh desktop.refresh(); // Show the surface Iw2DSurfaceShow(); // Yield to the opearting system s3eDeviceYield(duration); } } release(); return 0; }
[S3E] DataDirIsRAM=1
... - (data-ram/data-gles1, data) + (data-ram/data-gles1, data/data-gles1) locale_en.group.bin locale_ru.group.bin sounds.group.bin
Source: https://habr.com/ru/post/161959/
All Articles