fix: android comptatibility

This commit is contained in:
2021-11-02 11:11:51 +01:00
parent 0c68512caf
commit 1768b3473d
7 changed files with 188 additions and 45 deletions

View File

@@ -19,4 +19,61 @@ int main(int argc, const char* args[]) {
}
Display::destroy();
return 0;
}
}
#ifdef ANDROID
extern "C" {
int SDL_main(int argc, const char* args[]) {
return main(argc, args);
}
}
#endif
/*#include <SDL2/SDL.h>
int main(int argc, const char* argv[]){
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
int done;
SDL_Event event;
if (SDL_CreateWindowAndRenderer(0, 0, 0, &window, &renderer) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"SDL_CreateWindowAndRenderer() failed: %s", SDL_GetError());
return(2);
}
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't load texture: %s", SDL_GetError());
return(2);
}
SDL_SetWindowSize(window, 800, 480);
SDL_Rect rectangle;
rectangle.x = 100;
rectangle.y = 100;
rectangle.w = 300;
rectangle.h = 300;
done = 0;
while (!done) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT)
done = 1;
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawRect(renderer, &rectangle);
SDL_RenderPresent(renderer);
SDL_Delay(100);
}
SDL_DestroyTexture(texture);
SDL_Quit();
return 0;
}*/

View File

@@ -38,8 +38,7 @@ void init(SDL_Window* sdl_window, SDL_GLContext sdlContext, td::render::Renderer
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(sdl_window, gl_context);
const char* glslVersion = "#version 130";
ImGui_ImplOpenGL3_Init(glslVersion);
ImGui_ImplOpenGL3_Init();
ImFontConfig c;
c.SizePixels = 25;
ImGui::GetIO().Fonts->AddFontDefault(&c);
@@ -320,17 +319,8 @@ void tick() {
lastTime = td::utils::getTime();
}
void pollEvents(){
SDL_Event event;
while(SDL_PollEvent(&event)){
ImGui_ImplSDL2_ProcessEvent(&event);
}
}
void render() {
tick();
pollEvents();
beginFrame();
client->render();
if (client->isConnected())

View File

@@ -7,15 +7,18 @@
#include "render/shaders/EntityShader.h"
static const char vertexSource[] = R"(
#version 330
#ifdef ANDROID
static const char vertexSource[] =
R"(#version 300 es
in vec2 position;
in int color;
precision mediump float;
uniform vec2 camPos = vec2(0, 0);
uniform float zoom = 1;
uniform float aspectRatio = 0;
layout(location = 0) in vec2 position;
layout(location = 1) in int color;
uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform vec2 translation;
uniform float isometricView;
@@ -25,7 +28,51 @@ void main(void){
float modelX = position.x + translation.x;
float modelY = position.y + translation.y;
float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1 - isometricView)) * zoom;
float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
pass_color = color;
gl_Position = vec4(x, -y, 0.0, 1.0);
}
)";
static const char fragmentSource[] =
R"(#version 300 es
precision mediump float;
flat in int pass_color;
out vec4 out_color;
void main(void){
float r = float(pass_color >> 24 & 0xFF) / 255.0;
float g = float(pass_color >> 16 & 0xFF) / 255.0;
float b = float(pass_color >> 8 & 0xFF) / 255.0;
float a = float(pass_color & 0xFF) / 255.0;
out_color = vec4(r, g, b, a);
}
)";
#else
static const char vertexSource[] = R"(
#version 330
layout(location = 0) in vec2 position;
layout(location = 1) in int color;
uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform vec2 translation;
uniform float isometricView;
flat out int pass_color;
void main(void){
float modelX = position.x + translation.x;
float modelY = position.y + translation.y;
float x = (modelX - camPos.x - (modelY - camPos.y) * isometricView) / aspectRatio * zoom;
float y = ((0.5 * (modelX - camPos.x) + 0.5 * (modelY - camPos.y)) * isometricView + (modelY - camPos.y) * (1.0 - isometricView)) * zoom;
pass_color = color;
gl_Position = vec4(x, -y, 0.0, 1.0);
}
@@ -48,6 +95,7 @@ void main(void){
}
)";
#endif
EntityShader::EntityShader() : ShaderProgram() {}

View File

@@ -12,6 +12,10 @@
#include <glm/gtc/type_ptr.hpp>
#ifdef ANDROID
#include <android/log.h>
#endif
ShaderProgram::ShaderProgram() :
programID(0), vertexShaderID(0), fragmentShaderID(0) {
}
@@ -112,6 +116,9 @@ int ShaderProgram::loadShader(const std::string& source, GLenum type) {
char error[size];
glGetShaderInfoLog(shaderID, size, &size, error);
std::cout << error << std::endl;
#ifdef ANDROID
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "Could not compile shader !\n %s", error);
#endif
}
return shaderID;
}

View File

@@ -7,22 +7,66 @@
#include "render/shaders/WorldShader.h"
static const char vertexSource[] = R"(
#version 330
#ifdef ANDROID
static const char vertexSource[] =
R"(#version 300 es
precision mediump float;
layout(location = 0) in vec2 position;
layout(location = 1) in int color;
uniform vec2 camPos = vec2(0, 0);
uniform float zoom = 1;
uniform float aspectRatio = 0;
uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform float isometricView;
flat out int pass_color;
void main(void){
float x = (position.x - camPos.x - (position.y - camPos.y) * isometricView) / aspectRatio * zoom;
float y = ((0.5 * (position.x - camPos.x) + 0.5 * (position.y - camPos.y)) * isometricView + (position.y - camPos.y) * (1 - isometricView)) * zoom;
float y = ((0.5 * (position.x - camPos.x) + 0.5 * (position.y - camPos.y)) * isometricView + (position.y - camPos.y) * (1.0 - isometricView)) * zoom;
pass_color = color;
gl_Position = vec4(x, -y, 0.0, 1.0);
}
)";
static const char fragmentSource[] =
R"(#version 300 es
precision mediump float;
flat in int pass_color;
out vec4 out_color;
void main(void){
float r = float(pass_color >> 24 & 0xFF) / 255.0;
float g = float(pass_color >> 16 & 0xFF) / 255.0;
float b = float(pass_color >> 8 & 0xFF) / 255.0;
float a = float(pass_color & 0xFF) / 255.0;
out_color = vec4(r, g, b, a);
}
)";
#else
static const char vertexSource[] = R"(
#version 330
layout(location = 0) in vec2 position;
layout(location = 1) in int color;
uniform vec2 camPos;
uniform float zoom;
uniform float aspectRatio;
uniform float isometricView;
flat out int pass_color;
void main(void){
float x = (position.x - camPos.x - (position.y - camPos.y) * isometricView) / aspectRatio * zoom;
float y = ((0.5 * (position.x - camPos.x) + 0.5 * (position.y - camPos.y)) * isometricView + (position.y - camPos.y) * (1.0 - isometricView)) * zoom;
pass_color = color;
gl_Position = vec4(x, -y, 0.0, 1.0);
}
@@ -45,6 +89,7 @@ void main(void){
}
)";
#endif
WorldShader::WorldShader() : ShaderProgram() {}

View File

@@ -14,12 +14,19 @@
#include "game/GameManager.h"
#include "render/Renderer.h"
#include "render/WorldRenderer.h"
#include "../render/gui/imgui/imgui.h"
#include "../render/gui/imgui/imgui_impl_sdl.h"
#define WINDOW_NAME "Tower Defense"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#include <SDL2/SDL_render.h>
#ifdef ANDROID
#include <sstream>
#include <android/log.h>
#endif
namespace Display {
static SDL_Window* window;
@@ -32,10 +39,6 @@ static float aspectRatio;
static bool shouldClose = false;
void error_callback(int error, const char* description) {
std::cerr << "GLFW Error : " << description << std::endl;
}
void windowResizeEvent(int width, int height) {
aspectRatio = (float)width / height;
renderer->resize(width, height);
@@ -46,14 +49,14 @@ void windowResizeEvent(int width, int height) {
bool create() {
window = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
// Prepare and create context
#ifdef __ANDROID__
#ifdef ANDROID
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#endif
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
@@ -100,16 +103,7 @@ bool create() {
<< ", R" << r << "G" << g << "B" << b << "A" << a << ", depth bits: " << depth << std::endl;
SDL_GL_MakeCurrent(window, glContext);
/*Log(LOG_INFO) << "Finished initialization";
return ctx;
glfwSetErrorCallback(&error_callback);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_NAME, nullptr, nullptr);
glfwMakeContextCurrent(window);*/
if (!renderer->init()) {
exit(1);
}
@@ -132,6 +126,7 @@ void destroy() {
TowerGui::destroy();
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
window = NULL;
}
@@ -154,6 +149,7 @@ void pollEvents() {
break;
}
}
ImGui_ImplSDL2_ProcessEvent(&event);
}
}

View File

@@ -4,7 +4,7 @@ add_rules("mode.debug", "mode.release")
target("TowerDefense")
set_kind("binary")
add_includedirs("include", "src/render/gui")
add_includedirs("include")
add_files("src/*.cpp", "src/*/*.cpp", "src/*/*/*.cpp", "src/*/*/*/*.cpp")
add_cxflags("-pthread")