Added support for rendering on Android devices
This commit is contained in:
1 parent
e110b1f2fd
commit
2eb8d83dc0
8 files changed
+122
-14
No files matched your search
+15
-3
@@ -9,6 +9,7 @@
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_mixer.h"
|
||||
#include "SDL_pixels.h"
|
||||
#include "SDL_platform.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_surface.h"
|
||||
#include "SDL_timer.h"
|
||||
@@ -18,11 +19,11 @@
|
||||
#include "constants.h"
|
||||
#include "engine_build_config.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
#include <stdio.h>
|
||||
@@ -42,8 +43,11 @@ bool init_engine(const char *game_name, WindowSize window_size,
|
||||
SDL_Color window_background_color, bool full_screen,
|
||||
uint8_t audio_tracks) {
|
||||
bool initialisation_complete = true;
|
||||
const char *current_platform = SDL_GetPlatform();
|
||||
|
||||
SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");
|
||||
if (strcmp(current_platform, "Linux") == 0) {
|
||||
SDL_SetHint(SDL_HINT_VIDEODRIVER, "wayland,x11");
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
#ifdef DEBUG_ENGINE
|
||||
@@ -53,7 +57,15 @@ bool init_engine(const char *game_name, WindowSize window_size,
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t img_flags = IMG_INIT_PNG | IMG_INIT_TIF;
|
||||
uint32_t img_flags = IMG_INIT_PNG;
|
||||
if (strcmp(current_platform, "Android") != 0) {
|
||||
// Apparently, Android devices don't support TIFF images, so we only enable
|
||||
// TIFF support when we are not running Android.
|
||||
//
|
||||
// TODO (Abdelrahman): Check if iOS devices support TIFF images.
|
||||
img_flags |= IMG_INIT_TIF;
|
||||
}
|
||||
|
||||
if ((IMG_Init(img_flags) & img_flags) != img_flags) {
|
||||
#ifdef DEBUG_ENGINE
|
||||
printf("Failed to initialise SDL_image. Error: %s\n", IMG_GetError());
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "Renderer.h"
|
||||
#include "SDL.h"
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_platform.h"
|
||||
#include "SDL_rect.h"
|
||||
#include "SDL_render.h"
|
||||
#include "SDL_video.h"
|
||||
#include "engine_build_config.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
+47
-4
@@ -1,24 +1,55 @@
|
||||
#include "Window.h"
|
||||
#include "Renderer.h"
|
||||
#include "SDL_platform.h"
|
||||
#include "SDL_video.h"
|
||||
#include "engine_build_config.h"
|
||||
#include "math_utils.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
#include <stdio.h>
|
||||
#endif /* DEBUG_ENGINE */
|
||||
|
||||
typedef enum {
|
||||
MOBILE_ORIENTATION_LANDSCAPE,
|
||||
MOBILE_ORIENTATION_PORTRAIT
|
||||
} MobileOrientation;
|
||||
|
||||
bool init_window(Window *wnd, const char *title, WindowSize size,
|
||||
SDL_Color color, bool full_screen) {
|
||||
if (!wnd) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *current_platform = SDL_GetPlatform();
|
||||
|
||||
Uint32 window_flags = SDL_WINDOW_ALLOW_HIGHDPI;
|
||||
|
||||
if (full_screen) {
|
||||
if (strcmp(current_platform, "Android") == 0 ||
|
||||
strcmp(current_platform, "iOS") == 0) {
|
||||
// Calculate the mobile orientation based on the user-defined width and
|
||||
// height
|
||||
MobileOrientation orientation;
|
||||
|
||||
if (size.width > size.height) {
|
||||
orientation = MOBILE_ORIENTATION_LANDSCAPE;
|
||||
} else {
|
||||
orientation = MOBILE_ORIENTATION_PORTRAIT;
|
||||
}
|
||||
|
||||
SDL_DisplayMode mode;
|
||||
SDL_GetCurrentDisplayMode(0, &mode);
|
||||
|
||||
if (orientation == MOBILE_ORIENTATION_LANDSCAPE) {
|
||||
size.width = max_int(mode.w, mode.h);
|
||||
size.height = min_int(mode.w, mode.h);
|
||||
} else {
|
||||
size.width = min_int(mode.w, mode.h);
|
||||
size.height = max_int(mode.w, mode.h);
|
||||
}
|
||||
} else if (full_screen) {
|
||||
window_flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
}
|
||||
|
||||
@@ -52,7 +83,8 @@ bool init_window(Window *wnd, const char *title, WindowSize size,
|
||||
wnd->drawing_area.width = wnd->real_size.width;
|
||||
wnd->drawing_area.height = wnd->real_size.height;
|
||||
|
||||
if (full_screen) {
|
||||
if (full_screen && strcmp(current_platform, "Android") != 0 &&
|
||||
strcmp(current_platform, "iOS") != 0) {
|
||||
// Fit the window into the screen when running in full screen mode on
|
||||
// a non-mobile platform. Mobile platforms will be handled differently
|
||||
|
||||
@@ -84,6 +116,14 @@ bool init_window(Window *wnd, const char *title, WindowSize size,
|
||||
|
||||
wnd->defined_size.width = mode.w;
|
||||
wnd->defined_size.height = mode.h;
|
||||
} else if (strcmp(current_platform, "Android") == 0 ||
|
||||
strcmp(current_platform, "iOS") == 0) {
|
||||
// Get the correct real_size and drawing_area when running on a mobile
|
||||
// platform, since the get_render_output_size function doesn't work
|
||||
// properly on mobile
|
||||
|
||||
wnd->real_size.width = wnd->drawing_area.width = wnd->defined_size.width;
|
||||
wnd->real_size.height = wnd->drawing_area.height = wnd->defined_size.height;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
@@ -100,7 +140,10 @@ void clear_window(const Window *wnd) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (wnd->full_screen) {
|
||||
const char *current_platform = SDL_GetPlatform();
|
||||
|
||||
if (wnd->full_screen && strcmp(current_platform, "Android") != 0 &&
|
||||
strcmp(current_platform, "iOS") != 0) {
|
||||
// When rendering in full screen on a non-mobile platform, we fill the
|
||||
// entire screen with black, then fill an area that fits the window
|
||||
// dimensions with the user-defined backgroud color of the window
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "math_utils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
int32_t max_int(int32_t a, int32_t b) { return a >= b ? a : b; }
|
||||
|
||||
int32_t min_int(int32_t a, int32_t b) { return a <= b ? a : b; }
|
||||
Reference in new issue
Block a user