From d929df2ba6b352d57f1207965938be50509a92ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=A1=D1=82=D0=B5=D0=BF?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Tue, 11 Jul 2023 02:35:25 +0300 Subject: [PATCH] . --- android-project/app/build.gradle | 5 +- .../main/java/org/libsdl/app/SDLActivity.java | 6 +- src/core/config.h | 11 +-- src/core/event.h | 10 +-- src/engine/engine_opengl.cpp | 84 +++++++++--------- src/game.cpp | 87 +++++++++++-------- src/game.h | 36 ++++---- src/main.cpp | 2 +- 8 files changed, 130 insertions(+), 111 deletions(-) diff --git a/android-project/app/build.gradle b/android-project/app/build.gradle index 69eed1a..7e2a3fd 100644 --- a/android-project/app/build.gradle +++ b/android-project/app/build.gradle @@ -21,8 +21,8 @@ android { versionName "1.0" externalNativeBuild { cmake { - arguments "-DANDROID_STL=c++_shared", "-DBUILD_TYPE=Debug" - // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' + arguments "-DANDROID_STL=c++_shared", "-DBUILD_TYPE=Release" + abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64' // abiFilters 'arm64-v8a' } } @@ -34,6 +34,7 @@ android { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + signingConfig signingConfigs.debug } } applicationVariants.all { variant -> diff --git a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java index f9d7614..a348b97 100644 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java @@ -272,14 +272,14 @@ protected String getMainFunction() { protected String[] getLibraries() { return new String[] { "c++_shared", - "SDL3d", + "SDL3", // "SDL3_image", // "SDL3_mixer", // "SDL3_net", // "SDL3_ttf", // "assimpd", - "99-engined", - "99-gamed", + "99-engine", + "99-game" }; } diff --git a/src/core/config.h b/src/core/config.h index fa36336..bdfb178 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -18,10 +18,11 @@ struct config const char* model_board = "res/models/board.obj"; const char* model_cube = "res/models/cube.obj"; - double width = 1600 - 100; - double height = 900 - 100; - bool is_full_screen = false; + float width = 1600 - 100; + float height = 900 - 100; + bool is_full_screen = false; - float camera_speed_rotate = 1. / 100.; - float camera_speed = 0.05; + float camera_speed_rotate = 1. / 100.; + float camera_speed = 0.05; + float max_camera_speed_swipe = 10; }; \ No newline at end of file diff --git a/src/core/event.h b/src/core/event.h index f6bcc35..0fc507b 100644 --- a/src/core/event.h +++ b/src/core/event.h @@ -6,15 +6,15 @@ struct event { struct event_mouse { - uint8_t left_cliked : 1; + uint8_t left_clicked : 1; uint8_t left_released : 1; - uint8_t right_cliked : 1; + uint8_t right_clicked : 1; uint8_t right_released : 1; - uint8_t center_cliked : 1; + uint8_t center_clicked : 1; uint8_t center_released : 1; - uint8_t back_cliked : 1; + uint8_t back_clicked : 1; uint8_t back_released : 1; - uint8_t next_cliked : 1; + uint8_t next_clicked : 1; uint8_t next_released : 1; } mouse; diff --git a/src/engine/engine_opengl.cpp b/src/engine/engine_opengl.cpp index f4bb8a6..d537158 100644 --- a/src/engine/engine_opengl.cpp +++ b/src/engine/engine_opengl.cpp @@ -125,7 +125,7 @@ template void bind_vertexes() { glEnableVertexAttribArray(0); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); glVertexAttribPointer( 0, 3, @@ -140,7 +140,7 @@ template void bind_normal() { glEnableVertexAttribArray(1); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); glVertexAttribPointer( 1, 3, @@ -148,14 +148,14 @@ void bind_normal() GL_FALSE, sizeof(vertex_type), reinterpret_cast(vertex_type::OFFSET_NORMAL)); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); } template void bind_texture_coords() { glEnableVertexAttribArray(2); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); glVertexAttribPointer( 2, 2, @@ -163,21 +163,21 @@ void bind_texture_coords() GL_FALSE, sizeof(vertex_type), reinterpret_cast(vertex_type::OFFSET_TEXTURE)); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); } template void bind_colors() { glEnableVertexAttribArray(3); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(vertex_type), reinterpret_cast(vertex_type::OFFSET_COLOR)); - GL_CHECK_ERRORS() + GL_CHECK_ERRORS(); } void* load_gl_func(const char* name) @@ -232,25 +232,25 @@ void ImGui_ImplSdlGL3_RenderDrawLists(engine* eng, ImDrawData* draw_data) const ImDrawList* cmd_list = draw_data->CmdLists[n]; const ImDrawIdx* idx_buffer_offset = nullptr; - const vertex2d_colored_textured* vertex_data = + auto vertex_data = reinterpret_cast( cmd_list->VtxBuffer.Data); - size_t vert_count = static_cast(cmd_list->VtxBuffer.size()); + auto vert_count = static_cast(cmd_list->VtxBuffer.size()); - vertex_buffer* vertex_buff = + auto vertex_buff = new vertex_buffer(vertex_data, vert_count); const std::uint16_t* indexes = cmd_list->IdxBuffer.Data; - size_t index_count = static_cast(cmd_list->IdxBuffer.size()); + auto index_count = static_cast(cmd_list->IdxBuffer.size()); - index_buffer* index_buff = new index_buffer(indexes, index_count); + auto index_buff = new index_buffer(indexes, index_count); for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; - texture_opengl* tex = - reinterpret_cast(pcmd->TextureId); + auto tex = + static_cast(pcmd->TextureId); eng->render_triangles(vertex_buff, index_buff, tex, @@ -288,37 +288,41 @@ int engine_opengl::initialize(config& cfg) std::cout << "Window size: (" << cfg.width << " " << cfg.height << ")" << std::endl; +#ifdef __ANDROID__ + cfg.is_full_screen = true; +#endif + if (cfg.is_full_screen) { - const SDL_DisplayMode* dispale_mode = SDL_GetCurrentDisplayMode(1); - if (!dispale_mode) + const SDL_DisplayMode* display_mode = SDL_GetCurrentDisplayMode(1); + if (!display_mode) { std::cout << "can't get current display mode: " << SDL_GetError() << std::endl; } - cfg.width = dispale_mode->w; - cfg.height = dispale_mode->h; + cfg.width = display_mode->w; + cfg.height = display_mode->h; window = static_cast( SDL_CreateWindow(cfg.app_name, - cfg.width, - cfg.height, + static_cast(cfg.width), + static_cast(cfg.height), SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL)); } else { #ifdef __ANDROID__ - const SDL_DisplayMode* dispale_mode = SDL_GetCurrentDisplayMode(1); - if (!dispale_mode) + const SDL_DisplayMode* display_mode = SDL_GetCurrentDisplayMode(1); + if (!display_mode) { std::cout << "can't get current display mode: " << SDL_GetError() << std::endl; } - cfg.width = dispale_mode->w; - cfg.height = dispale_mode->h; + cfg.width = display_mode->w; + cfg.height = display_mode->h; #endif window = static_cast(SDL_CreateWindow( - cfg.app_name, cfg.width, cfg.height, SDL_WINDOW_OPENGL)); + cfg.app_name, static_cast(cfg.width), static_cast(cfg.height), SDL_WINDOW_OPENGL)); } std::cout << "Window size: (" << cfg.width << " " << cfg.height << ")" << std::endl; @@ -514,7 +518,7 @@ bool engine_opengl::event_keyboard(event& e) break; case SDL_EVENT_MOUSE_BUTTON_DOWN: // clang-format off - if (sdl_event.button.button == SDL_BUTTON_LEFT) e.mouse.left_cliked = 1; + if (sdl_event.button.button == SDL_BUTTON_LEFT) e.mouse.left_clicked = 1; // clang-format on is_event = true; break; @@ -605,7 +609,7 @@ void engine_opengl::render_triangles(vertex_buffer* vertexes, bind_normal(); glDrawElements( - GL_TRIANGLES, num_vertexes, GL_UNSIGNED_SHORT, start_vertex_index); + GL_TRIANGLES, static_cast(num_vertexes), GL_UNSIGNED_SHORT, start_vertex_index); GL_CHECK_ERRORS() } void engine_opengl::render_triangles(vertex_buffer* vertexes, @@ -623,7 +627,7 @@ void engine_opengl::render_triangles(vertex_buffer* vertexes, bind_colors(); glDrawElements( - GL_TRIANGLES, num_vertexes, GL_UNSIGNED_SHORT, start_vertex_index); + GL_TRIANGLES, static_cast(num_vertexes), GL_UNSIGNED_SHORT, start_vertex_index); GL_CHECK_ERRORS() } void engine_opengl::render_triangles(vertex_buffer* vertexes, @@ -643,7 +647,7 @@ void engine_opengl::render_triangles(vertex_buffer* vertexes, bind_texture_coords(); glDrawElements( - GL_TRIANGLES, num_vertexes, GL_UNSIGNED_SHORT, start_vertex_index); + GL_TRIANGLES, static_cast(num_vertexes), GL_UNSIGNED_SHORT, start_vertex_index); GL_CHECK_ERRORS() } @@ -664,7 +668,7 @@ void engine_opengl::render_triangles( bind_colors(); glDrawElements( - GL_TRIANGLES, num_vertexes, GL_UNSIGNED_SHORT, start_vertex_index); + GL_TRIANGLES, static_cast(num_vertexes), GL_UNSIGNED_SHORT, start_vertex_index); GL_CHECK_ERRORS() } @@ -684,7 +688,7 @@ void engine_opengl::render_triangles( bind_colors(); glDrawElements( - GL_TRIANGLES, num_vertexes, GL_UNSIGNED_SHORT, start_vertex_index); + GL_TRIANGLES, static_cast(num_vertexes), GL_UNSIGNED_SHORT, start_vertex_index); GL_CHECK_ERRORS() } @@ -772,7 +776,7 @@ void engine_opengl::audio_callback(void* engine_ptr, std::fill_n(stream, stream_size, '\0'); - engine_opengl* e = static_cast(engine_ptr); + auto e = static_cast(engine_ptr); for (audio_buffer* buff : e->audio_output) { @@ -834,7 +838,7 @@ void engine_opengl::play_sound(const char* path, bool is_looped) { std::lock_guard lock(audio_mutex); - audio_buffer* audio_buff = + auto audio_buff = new audio_buffer(path, audio_device, audio_device_spec, is_looped); audio_buff->current_index = 0; @@ -899,8 +903,8 @@ bool ImGui_ImplSdlGL3_ProcessEvent(const SDL_Event* event, config& cfg) { int mouse_button = 0; - ImVec2 mouse_pos((float)event->tfinger.x * cfg.width, - (float)event->tfinger.y * cfg.height); + ImVec2 mouse_pos(event->tfinger.x * cfg.width, + event->tfinger.y * cfg.height); io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); io.AddMouseSourceEvent(ImGuiMouseSource_TouchScreen); io.AddMouseButtonEvent(mouse_button, @@ -944,7 +948,7 @@ bool ImGui_ImplSdlGL3_CreateDeviceObjects(config& cfg) void ImGui_ImplSdlGL3_InvalidateDeviceObjects() { void* ptr = ImGui::GetIO().Fonts->TexID; - texture_opengl* texture = reinterpret_cast(ptr); + auto texture = reinterpret_cast(ptr); delete g_imgui_shader; g_imgui_shader = nullptr; @@ -1014,7 +1018,7 @@ bool ImGui_ImplSdlGL3_Init(SDL_Window* window, config& cfg) // (void)window; // #endif - g_Time = SDL_GetTicks() / 1000.f; + g_Time = static_cast(SDL_GetTicks()) / 1000.f; if (io.Fonts->TexID == nullptr) { @@ -1040,12 +1044,12 @@ void ImGui_ImplSdlGL3_NewFrame(SDL_Window* window) SDL_GetWindowSize(window, &w, &h); SDL_GetWindowSizeInPixels(window, &display_w, &display_h); io.DisplaySize = ImVec2(float(w), float(h)); - io.DisplayFramebufferScale = ImVec2(w > 0 ? float(display_w / w) : 0.f, - h > 0 ? float(display_h / h) : 0.f); + io.DisplayFramebufferScale = ImVec2(w > 0 ? static_cast(display_w) / w : 0.f, + h > 0 ? static_cast(display_h) / h : 0.f); // Setup time step Uint32 time = SDL_GetTicks(); - float current_time = time / 1000.0f; + float current_time = static_cast(time) / 1000.0f; io.DeltaTime = current_time - g_Time; // (1.0f / 60.0f); if (io.DeltaTime <= 0) { diff --git a/src/game.cpp b/src/game.cpp index 56a26d5..d47cba4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -40,18 +40,18 @@ int game_tetris::initialize(config _cfg) add_figure(figure_board, texture_board); my_engine->play_sound(cfg.sound_background_music, true); - window_score_width = 0.1 * cfg.width; - window_score_height = 0.03 * cfg.height; - window_score_x = 10; - window_score_y = 10; - - window_control_width = 0.30 * cfg.width; - window_control_height = 0.35 * cfg.height; - window_control_x = 20; - window_control_y = 0.6 * cfg.height; - window_rotate_width = 0.2 * cfg.width; - window_rotate_height = 0.95 * cfg.height; - window_rotate_x = 0.8 * cfg.width - 10; + window_score_width = 0.1f * cfg.width; + window_score_height = 0.03f * cfg.height; + window_score_x = 10.f; + window_score_y = 10.f; + + window_control_width = 0.30f * cfg.width; + window_control_height = 0.35f * cfg.height; + window_control_x = 20.f; + window_control_y = 0.6f * cfg.height; + window_rotate_width = 0.2f * cfg.width; + window_rotate_height = 0.95f * cfg.height; + window_rotate_x = 0.8f * cfg.width - 10; window_rotate_y = 10; return 1; @@ -70,7 +70,7 @@ bool game_tetris::event_listener(event& e) // Only game buttons if (state.is_started) { - if (e.mouse.left_cliked) + if (e.mouse.left_clicked) { state.is_rotated = true; state.is_moving = true; @@ -80,29 +80,33 @@ bool game_tetris::event_listener(event& e) state.is_rotated = false; state.is_moving = false; } - if (e.motion.x_rel && state.is_rotated) - { #ifdef __ANDROID__ - if (e.motion.x > window_control_width && - e.motion.x < window_rotate_x) + if (e.motion.x > window_control_width && + e.motion.x < window_rotate_x && !e.mouse.left_clicked) #endif - camera_angle += e.motion.x_rel * M_PI / 300; - } - if (e.motion.y_rel && state.is_rotated) { -#ifdef __ANDROID__ - if (e.motion.x > window_control_width && - e.motion.x < window_rotate_x) - { -#endif + if (e.motion.x_rel && state.is_rotated) { + + + if (abs(e.motion.x_rel) > cfg.max_camera_speed_swipe) + e.motion.x_rel = std::copysign(cfg.max_camera_speed_swipe, e.motion.x_rel); + + camera_angle += e.motion.x_rel * M_PI / 300; + + } + if (e.motion.y_rel && state.is_rotated) { + + + if (abs(e.motion.y_rel) > cfg.max_camera_speed_swipe) + e.motion.y_rel = std::copysign(cfg.max_camera_speed_swipe, e.motion.y_rel); + view_height += e.motion.y_rel / 50; if (view_height < min_view_height) view_height = min_view_height; if (view_height > max_view_height) view_height = max_view_height; -#ifdef __ANDROID__ + } -#endif } if (e.keyboard.w_clicked) @@ -245,16 +249,17 @@ void game_tetris::draw_menu() static const int window_height = 0.2 * cfg.height; static const int window_x = (cfg.width - window_width) / 2; static const int window_y = (cfg.height - window_height) / 2; + static ImVec2 button_size = ImVec2(window_width - 15, window_height / 2 - 15); ImGui::SetNextWindowSize(ImVec2(window_width, window_height)); ImGui::SetNextWindowPos(ImVec2(window_x, window_y)); ImGui::Begin("Menu", - 0, + nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar); - if (ImGui::Button("Start", ImVec2(0.1 * cfg.width, 0.05 * cfg.height))) + if (ImGui::Button("Start", button_size)) { start_game(); } @@ -262,24 +267,25 @@ void game_tetris::draw_menu() // cfg.height))) // { // } - if (ImGui::Button("Quit", ImVec2(0.09 * cfg.width, 0.05 * cfg.height))) + if (ImGui::Button("Quit", button_size)) { + state.is_quit = true; } ImGui::End(); } void game_tetris::draw_restart_menu() { - static const int window_width = 0.2 * cfg.width; - static const int window_height = 0.2 * cfg.height; - static const int window_x = (cfg.width - window_width) / 2; - static const int window_y = (cfg.height - window_height) / 2; + static const float window_width = 0.2f * cfg.width; + static const float window_height = 0.2f * cfg.height; + static const float window_x = (cfg.width - window_width) / 2; + static const float window_y = (cfg.height - window_height) / 2; ImGui::SetNextWindowSize(ImVec2(window_width, window_height)); ImGui::SetNextWindowPos(ImVec2(window_x, window_y)); ImGui::Begin("Restart", - 0, + nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar); @@ -295,6 +301,7 @@ void game_tetris::draw_restart_menu() // } if (ImGui::Button("Quit", ImVec2(0.09 * cfg.width, 0.05 * cfg.height))) { + state.is_quit = true; } ImGui::End(); @@ -306,7 +313,7 @@ void game_tetris::draw_ui() ImGui::SetNextWindowPos(ImVec2(window_score_x, window_score_y)); ImGui::Begin("State", - 0, + nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar); @@ -325,7 +332,7 @@ void game_tetris::draw_ui() ImGui::SetNextWindowPos(ImVec2(window_control_x, window_control_y)); ImGui::Begin("Controller", - 0, + nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoBackground); @@ -358,7 +365,7 @@ void game_tetris::draw_ui() ImGui::SetNextWindowPos(ImVec2(window_rotate_x, window_rotate_y)); ImGui::Begin("Rotate", - 0, + nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoBackground); @@ -694,3 +701,7 @@ void game_tetris::check_layer() } } } + +bool game_tetris::get_quit_state() const { + return state.is_quit; +} diff --git a/src/game.h b/src/game.h index b4a1e61..43b382c 100644 --- a/src/game.h +++ b/src/game.h @@ -143,6 +143,8 @@ class game_tetris : public game void add_figure(figure* fig, texture* texture); + bool get_quit_state() const; + private: void draw_menu(); void draw_restart_menu(); @@ -162,7 +164,7 @@ class game_tetris : public game config cfg; size_t score = 0; - float delay = 1; // Seconds + float delay = 0.6; // Seconds uniform uniforms; figure* figure_board; @@ -176,10 +178,10 @@ class game_tetris : public game texture* texture_board = nullptr; std::vector textures_block; - double camera_angle = -M_PI / 2; - double view_height = 1.; - double min_view_height = 1.; - double max_view_height = 1.6; + float camera_angle = -M_PI / 2; + float view_height = 1.; + float min_view_height = 1.; + float max_view_height = 1.6; struct flags { @@ -191,16 +193,16 @@ class game_tetris : public game } state; - int window_score_width; - int window_score_height; - int window_score_x; - int window_score_y; - int window_control_width; - int window_control_height; - int window_control_x; - int window_control_y; - int window_rotate_width; - int window_rotate_height; - int window_rotate_x; - int window_rotate_y; + float window_score_width; + float window_score_height; + float window_score_x; + float window_score_y; + float window_control_width; + float window_control_height; + float window_control_x; + float window_control_y; + float window_rotate_width; + float window_rotate_height; + float window_rotate_x; + float window_rotate_y; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a2c3f5a..cf4fca5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -62,7 +62,7 @@ int main(int argc, char* argv[]) event e{}; auto time_last = std::chrono::steady_clock::now() - std::chrono::seconds(1); - while (my_game.event_listener(e)) + while (my_game.event_listener(e) && !my_game.get_quit_state()) { auto time_now = std::chrono::steady_clock::now();