[ion/simulator] Sort the platform functions

This commit is contained in:
Romain Goyet
2020-09-04 21:50:04 -04:00
committed by Léa Saviot
parent 0116dc2e07
commit 09a7b9daca
16 changed files with 100 additions and 41 deletions

View File

@@ -1,6 +1,6 @@
ion_src += $(addprefix ion/src/simulator/android/src/cpp/, \
images.cpp \
haptics_enabled.cpp \
platform_images.cpp \
)
ion_src += $(addprefix ion/src/simulator/shared/, \

View File

@@ -4,7 +4,11 @@
#include <android/bitmap.h>
#include <assert.h>
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
namespace Ion {
namespace Simulator {
namespace Platform {
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) {
JNIEnv * env = static_cast<JNIEnv *>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
@@ -54,3 +58,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
env->DeleteLocalRef(activity);
return texture;
}
}
}
}

View File

@@ -1,9 +1,9 @@
ion_src += $(addprefix ion/src/simulator/ios/, \
images.m \
platform_images.mm \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
apple/language.m \
apple/platform_language.mm \
dummy/haptics_enabled.cpp \
dummy/journal.cpp \
dummy/keyboard_callback.cpp \

View File

@@ -1,9 +1,12 @@
#include "../shared/platform.h"
#include <SDL.h>
#include <UIKit/UIKit.h>
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
namespace Ion {
namespace Simulator {
namespace Platform {
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) {
CGImageRef cgImage = [[UIImage imageNamed:[NSString stringWithUTF8String:identifier]] CGImage];
if (cgImage == NULL) {
return NULL;
@@ -53,3 +56,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
return texture;
}
}
}
}

View File

@@ -8,9 +8,9 @@ SFLAGS += -DUSING_GENERATED_CONFIG_H
SFLAGS += -Iion/src/simulator/linux/include
ion_src += $(addprefix ion/src/simulator/linux/, \
images.cpp \
language.cpp \
assets.s \
platform_images.cpp \
platform_language.cpp \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
@@ -32,13 +32,13 @@ LDFLAGS += -ljpeg
$(eval $(call rule_for, \
INCBIN, \
ion/src/simulator/linux/assets.s ion/src/simulator/linux/images.h, \
ion/src/simulator/linux/assets.s ion/src/simulator/linux/platform_images.h, \
$(ion_simulator_assets_paths), \
$$(PYTHON) ion/src/simulator/linux/incbin.py $(ion_simulator_assets) -o $$@, \
global \
))
$(call object_for,ion/src/simulator/linux/images.cpp): $(BUILD_DIR)/ion/src/simulator/linux/images.h
$(call object_for,ion/src/simulator/linux/platform_images.cpp): $(BUILD_DIR)/ion/src/simulator/linux/platform_images.h
# The header is refered to as <ion/src/simulator/linux/images.h> so make sure it's findable this way
# The header is refered to as <ion/src/simulator/linux/platform_images.h> so make sure it's findable this way
SFLAGS += -I$(BUILD_DIR)

View File

@@ -5,7 +5,11 @@
#include <png.h>
#include <assert.h>
#include <ion/src/simulator/linux/images.h>
#include <ion/src/simulator/linux/platform_images.h>
namespace Ion {
namespace Simulator {
namespace Platform {
enum class AssetFormat {
JPG,
@@ -95,7 +99,7 @@ bool readJPG(const unsigned char * start, size_t size, unsigned char ** bitmapDa
return true;
}
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) {
static constexpr const char * jpgExtension = ".jpg";
static constexpr const char * pngExtension = ".png";
@@ -161,3 +165,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
return texture;
}
}
}
}

View File

@@ -1,8 +1,11 @@
#include "../shared/platform.h"
#include <locale.h>
char * IonSimulatorGetLanguageCode() {
namespace Ion {
namespace Simulator {
namespace Platform {
const char * languageCode() {
static char buffer[3] = {0};
char * locale = setlocale(LC_ALL, "");
if (locale[2] == '_') {
@@ -12,3 +15,7 @@ char * IonSimulatorGetLanguageCode() {
}
return nullptr;
}
}
}
}

View File

@@ -1,9 +1,9 @@
ion_src += $(addprefix ion/src/simulator/macos/, \
images.m \
platform_images.mm \
)
ion_src += $(addprefix ion/src/simulator/shared/, \
apple/language.m \
apple/platform_language.mm \
dummy/haptics_enabled.cpp \
dummy/keyboard_callback.cpp \
dummy/window_callback.cpp \

View File

@@ -3,7 +3,11 @@
#include <SDL.h>
#include <AppKit/AppKit.h>
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
namespace Ion {
namespace Simulator {
namespace Platform {
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) {
NSImage * nsImage = [NSImage imageNamed:[NSString stringWithUTF8String:identifier]];
CGImageRef cgImage = [nsImage CGImageForProposedRect:NULL
context:NULL
@@ -55,3 +59,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
return texture;
}
}
}
}

View File

@@ -2,7 +2,11 @@
#include <Foundation/Foundation.h>
char * IonSimulatorGetLanguageCode() {
namespace Ion {
namespace Simulator {
namespace Platform {
const char * languageCode() {
static char buffer[4] = {0};
if (buffer[0] == 0) {
NSString * preferredLanguage = [[NSLocale preferredLanguages] firstObject];
@@ -15,3 +19,7 @@ char * IonSimulatorGetLanguageCode() {
}
return buffer;
}
}
}
}

View File

@@ -202,9 +202,9 @@ static SDL_Texture * sBackgroundTexture = nullptr;
static SDL_Texture * sKeyLayoutTextures[KeyLayout::NumberOfShapes];
void init(SDL_Renderer * renderer) {
sBackgroundTexture = IonSimulatorLoadImage(renderer, "background.jpg");
sBackgroundTexture = Platform::loadImage(renderer, "background.jpg");
for (size_t i = 0; i < KeyLayout::NumberOfShapes; i++) {
sKeyLayoutTextures[i] = IonSimulatorLoadImage(renderer, KeyLayout::assetName[i]);
sKeyLayoutTextures[i] = Platform::loadImage(renderer, KeyLayout::assetName[i]);
}
}

View File

@@ -4,6 +4,7 @@
#include "random.h"
#include "telemetry.h"
#include "window.h"
#include <algorithm>
#include <vector>
#include <ion.h>
#ifndef __WIN32__
@@ -68,11 +69,13 @@ std::vector<const char *>::const_iterator Args::find(const char * name) const {
);
}
using namespace Ion::Simulator;
int main(int argc, char * argv[]) {
Args args(argc, argv);
if (!args.has("--language")) {
args.push("--language", IonSimulatorGetLanguageCode());
args.push("--language", Platform::languageCode());
}
#ifndef __WIN32__
@@ -88,7 +91,6 @@ int main(int argc, char * argv[]) {
bool headless = args.popFlag("--headless");
using namespace Ion::Simulator;
Random::init();
if (!headless) {
Journal::init();

View File

@@ -2,20 +2,16 @@
#define ION_SIMULATOR_PLATFORM_H
#include <SDL.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
namespace Ion {
namespace Simulator {
namespace Platform {
/* Those functions should be implemented per-platform.
* They are defined as C function for easier interop. */
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier);
const char * languageCode();
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier);
char * IonSimulatorGetLanguageCode();
#ifdef __cplusplus
}
#endif
}
}
#endif

View File

@@ -1,6 +1,6 @@
ion_src += $(addprefix ion/src/simulator/windows/, \
images.cpp \
language.cpp \
platform_images.cpp \
platform_language.cpp \
resources.rc \
)
@@ -36,6 +36,6 @@ $(eval $(call rule_for, \
global \
))
$(call object_for,ion/src/simulator/windows/images.cpp): $(BUILD_DIR)/ion/src/simulator/windows/images.h
$(call object_for,ion/src/simulator/windows/platform_images.cpp): $(BUILD_DIR)/ion/src/simulator/windows/images.h
$(call object_for,ion/src/simulator/windows/resources.rc): $(BUILD_DIR)/ion/src/simulator/windows/resources_gen.rc

View File

@@ -12,7 +12,7 @@
* Note that this adds an extra runtime dependency (as compared to just SDL),
* but this should not be an issue. */
HRESULT CreateStreamOnResource(const char * name, LPSTREAM * stream) {
static inline HRESULT CreateStreamOnResource(const char * name, LPSTREAM * stream) {
HINSTANCE hInstance = GetModuleHandle(0);
*stream = nullptr;
HRSRC hC = FindResource(hInstance, name, RT_RCDATA);
@@ -36,7 +36,11 @@ HRESULT CreateStreamOnResource(const char * name, LPSTREAM * stream) {
return hr;
}
SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identifier) {
namespace Ion {
namespace Simulator {
namespace Platform {
SDL_Texture * loadImage(SDL_Renderer * renderer, const char * identifier) {
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
@@ -87,3 +91,7 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi
return texture;
}
}
}
}

View File

@@ -1,8 +1,11 @@
#include "../shared/platform.h"
#include <winnls.h>
char * IonSimulatorGetLanguageCode() {
namespace Ion {
namespace Simulator {
namespace Platform {
const char * languageCode() {
/* Per documentation, the maximum number of characters allowed for the
* language string is nine, including a terminating null character. */
static char buffer[9] = {0};
@@ -19,3 +22,7 @@ char * IonSimulatorGetLanguageCode() {
}
return buffer;
}
}
}
}