185 lines
5.3 KiB
C++
185 lines
5.3 KiB
C++
#include "Game.h"
|
||
#include "Environment.h"
|
||
#include <iostream>
|
||
#ifdef __ANDROID__
|
||
#include <android/log.h>
|
||
#endif
|
||
|
||
|
||
ZL::Game game;
|
||
|
||
|
||
void MainLoop() {
|
||
game.update();
|
||
}
|
||
|
||
|
||
#ifdef __ANDROID__
|
||
|
||
extern "C" int SDL_main(int argc, char* argv[]) {
|
||
// Инициализация SDL перед получением информации о дисплее
|
||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "SDL init failed: %s", SDL_GetError());
|
||
return 1;
|
||
}
|
||
|
||
SDL_DisplayMode displayMode;
|
||
if (SDL_GetCurrentDisplayMode(0, &displayMode) != 0) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "SDL_GetCurrentDisplayMode failed: %s", SDL_GetError());
|
||
SDL_Quit();
|
||
return 1;
|
||
}
|
||
ZL::Environment::width = displayMode.w;
|
||
ZL::Environment::height = displayMode.h;
|
||
|
||
__android_log_print(ANDROID_LOG_INFO, "Game", "Display resolution: %dx%d",
|
||
ZL::Environment::width, ZL::Environment::height);
|
||
|
||
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_MINOR_VERSION, 0);
|
||
|
||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
|
||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
|
||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
|
||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||
|
||
|
||
ZL::Environment::window = SDL_CreateWindow(
|
||
"Space Ship Game",
|
||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||
ZL::Environment::width, ZL::Environment::height,
|
||
SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
|
||
);
|
||
|
||
if (!ZL::Environment::window) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "Failed to create window: %s", SDL_GetError());
|
||
SDL_Quit();
|
||
return 1;
|
||
}
|
||
|
||
SDL_GLContext ctx = SDL_GL_CreateContext(ZL::Environment::window);
|
||
if (!ctx) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "SDL_GL_CreateContext failed: %s", SDL_GetError());
|
||
SDL_DestroyWindow(ZL::Environment::window);
|
||
SDL_Quit();
|
||
return 1;
|
||
}
|
||
|
||
if (SDL_GL_MakeCurrent(ZL::Environment::window, ctx) != 0) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "SDL_GL_MakeCurrent failed: %s", SDL_GetError());
|
||
SDL_GL_DeleteContext(ctx);
|
||
SDL_DestroyWindow(ZL::Environment::window);
|
||
SDL_Quit();
|
||
return 1;
|
||
}
|
||
|
||
// Настройка VSync
|
||
if (SDL_GL_SetSwapInterval(1) < 0) {
|
||
__android_log_print(ANDROID_LOG_WARN, "Game", "Unable to set VSync: %s", SDL_GetError());
|
||
}
|
||
|
||
// Настройка игры
|
||
try {
|
||
game.setup();
|
||
} catch (const std::exception &e) {
|
||
__android_log_print(ANDROID_LOG_ERROR, "Game", "Game setup failed: %s", e.what());
|
||
return 1;
|
||
}
|
||
|
||
while (!game.shouldExit()) {
|
||
game.update();
|
||
SDL_Delay(16);
|
||
}
|
||
|
||
return 0;
|
||
|
||
}
|
||
|
||
|
||
#else
|
||
|
||
int main(int argc, char *argv[]) {
|
||
try
|
||
{
|
||
|
||
|
||
constexpr int CONST_WIDTH = 1280;
|
||
constexpr int CONST_HEIGHT = 720;
|
||
|
||
ZL::Environment::width = CONST_WIDTH;
|
||
ZL::Environment::height = CONST_HEIGHT;
|
||
|
||
|
||
#ifdef EMSCRIPTEN
|
||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||
std::cerr << "SDL_Init failed: " << SDL_GetError() << std::endl;
|
||
return 1;
|
||
}
|
||
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||
|
||
SDL_Window* win = SDL_CreateWindow("Space Ship Game",
|
||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||
CONST_WIDTH, CONST_HEIGHT,
|
||
SDL_WINDOW_OPENGL);
|
||
|
||
if (!win) {
|
||
std::cerr << "SDL_CreateWindow failed: " << SDL_GetError() << std::endl;
|
||
return 1;
|
||
}
|
||
|
||
SDL_GLContext glContext = SDL_GL_CreateContext(win);
|
||
if (!glContext) {
|
||
std::cerr << "SDL_GL_CreateContext failed: " << SDL_GetError() << std::endl;
|
||
return 1;
|
||
}
|
||
|
||
// Привязка контекста к окну — важно!
|
||
SDL_GL_MakeCurrent(win, glContext);
|
||
|
||
ZL::Environment::window = win;
|
||
#else
|
||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
|
||
SDL_Log("SDL init failed: %s", SDL_GetError());
|
||
return 1;
|
||
}
|
||
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||
|
||
ZL::Environment::window = SDL_CreateWindow(
|
||
"Space Ship Game",
|
||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||
CONST_WIDTH, CONST_HEIGHT,
|
||
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
|
||
);
|
||
|
||
SDL_GLContext ctx = SDL_GL_CreateContext(ZL::Environment::window);
|
||
SDL_GL_MakeCurrent(ZL::Environment::window, ctx);
|
||
#endif
|
||
|
||
game.setup();
|
||
|
||
#ifdef EMSCRIPTEN
|
||
emscripten_set_main_loop(MainLoop, 0, 1);
|
||
#else
|
||
while (!game.shouldExit()) {
|
||
game.update();
|
||
SDL_Delay(2);
|
||
}
|
||
#endif
|
||
|
||
}
|
||
catch (const std::exception& e)
|
||
{
|
||
std::cout << e.what() << std::endl;
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
#endif |