diff options
author | Sven Göthel <[email protected]> | 2024-05-12 08:41:53 +0200 |
---|---|---|
committer | Sven Göthel <[email protected]> | 2024-05-12 08:41:53 +0200 |
commit | 61610043963f846406315aa662931608a1a9d9cf (patch) | |
tree | 734417e5e2b279511a426e53c80aa7a170e50df3 | |
parent | 8a052037be51a69496da8adc015549428565edf1 (diff) |
Cleanup (format, (c) year + author, ..)
-rw-r--r-- | examples/redsquare01.cpp | 4 | ||||
-rw-r--r-- | include/gamp/gamp.hpp | 137 | ||||
-rw-r--r-- | include/gamp/gamp_types.hpp | 63 | ||||
-rw-r--r-- | src/gamp.cpp | 4 | ||||
-rw-r--r-- | src/sdl_subsys.cpp | 238 | ||||
-rw-r--r-- | test/placeholder.txt | 1 |
6 files changed, 246 insertions, 201 deletions
diff --git a/examples/redsquare01.cpp b/examples/redsquare01.cpp index 064dcbf..bba4eb9 100644 --- a/examples/redsquare01.cpp +++ b/examples/redsquare01.cpp @@ -1,6 +1,6 @@ /* - * Author: Sven Gothel <[email protected]> - * Copyright (c) 2022 Gothel Software e.K. + * Author: Sven Gothel <[email protected]> and Svenson Han Gothel + * Copyright (c) 2022-2024 Gothel Software e.K. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/include/gamp/gamp.hpp b/include/gamp/gamp.hpp index 5cd7ab5..679f680 100644 --- a/include/gamp/gamp.hpp +++ b/include/gamp/gamp.hpp @@ -1,6 +1,6 @@ /* - * Author: Sven Gothel <[email protected]> - * Copyright (c) 2022 Gothel Software e.K. + * Author: Sven Gothel <[email protected]> and Svenson Han Gothel + * Copyright (c) 2022-2024 Gothel Software e.K. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -21,41 +21,10 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#ifndef GAMP_HPP_ -#define GAMP_HPP_ +#ifndef JAU_GAMP_HPP_ +#define JAU_GAMP_HPP_ -#include <cinttypes> -#include <cmath> -#include <cstdarg> -#include <cstdint> -#include <cstdio> -#include <cstring> -#include <functional> -#include <limits> -#include <memory> -#include <string> -#include <type_traits> -#include <vector> -#include <iostream> -#include <cctype> - -#include <jau/environment.hpp> -#include <jau/os/os_support.hpp> -#include <jau/float_math.hpp> - -#include <jau/math/vec2f.hpp> -#include <jau/math/vec3f.hpp> -#include <jau/math/vec4f.hpp> -#include <jau/math/mat4f.hpp> -#include <jau/math/recti.hpp> - -#include <jau/math/util/pmvmat4f.hpp> - -#if defined(__EMSCRIPTEN__) - #include <emscripten.h> -#else - #define EMSCRIPTEN_KEEPALIVE -#endif +#include <gamp/gamp_types.hpp> /** * Basic computer graphics math and utilities helping with the framebuffer and I/O tooling. @@ -67,16 +36,14 @@ namespace gamp { extern int win_height; /** Ratio pixel-size / window-size, a DPI derivative per axis*/ extern float devicePixelRatio[2]; - + /** * Width of the framebuffer coordinate in pixels. * * Framebuffer origin 0/0 is top-left corner. */ extern jau::math::Recti viewport; - - typedef std::vector<uint32_t> pixel_buffer_t; // 32-bit pixel - extern pixel_buffer_t fb_pixels; + /** Display frames per seconds */ extern int display_frames_per_sec; /** Optional custom forced frames per seconds, pass to swap_gpu_buffer() by default. Defaults to -1, i.e. automatic fps. */ @@ -88,12 +55,12 @@ namespace gamp { // /** GFX Toolkit: Initialize a window of given size with a usable framebuffer. */ - bool init_gfx_subsystem(const char* title, int window_width, int window_height, bool enable_vsync=true); + bool init_gfx_subsystem(const char* title, int window_width, int window_height, bool enable_vsync = true); /** GFX Toolkit: Swap GPU back to front framebuffer using given fps, maintaining vertical monitor synchronization if possible. fps <= 0 implies automatic fps. */ void swap_gpu_buffer(int fps) noexcept; /** GFX Toolkit: Swap GPU back to front framebuffer using forced_fps, maintaining vertical monitor synchronization if possible. */ inline void swap_gpu_buffer() noexcept { swap_gpu_buffer(forced_fps); } - + /** Returns frames per seconds, averaged over get_gpu_stat_period(). */ float get_gpu_stats_fps() noexcept; /** Returns rendering costs per frame in seconds, averaged over get_gpu_stat_period(). */ @@ -108,7 +75,7 @@ namespace gamp { void set_gpu_stats_show(bool enable) noexcept; /** Returns whether statistics are printed on the console, see set_show_gpu_stats(). */ bool get_gpu_stats_show() noexcept; - + // // input // @@ -120,14 +87,14 @@ namespace gamp { POINTER_MOTION, ANY_KEY_UP, ANY_KEY_DOWN, - P1_UP, // 5 + P1_UP, // 5 P1_DOWN, P1_RIGHT, P1_LEFT, P1_ACTION1, P1_ACTION2, P1_ACTION3, - PAUSE, // 11 + PAUSE, // 11 P2_UP, P2_DOWN, P2_RIGHT, @@ -135,10 +102,10 @@ namespace gamp { P2_ACTION1, P2_ACTION2, P2_ACTION3, - RESET, // 18 + RESET, // 18 /** Request to close window, which then should be closed by caller */ WINDOW_CLOSE_REQ, - WINDOW_RESIZED, // 20 + WINDOW_RESIZED, // 20 }; constexpr int bitno(const input_event_type_t e) noexcept { return static_cast<int>(e) - static_cast<int>(input_event_type_t::P1_UP); @@ -157,25 +124,26 @@ namespace gamp { class input_event_t { private: constexpr static const uint32_t p1_mask = - bitmask(input_event_type_t::P1_UP) | - bitmask(input_event_type_t::P1_DOWN) | - bitmask(input_event_type_t::P1_RIGHT) | - bitmask(input_event_type_t::P1_LEFT) | - bitmask(input_event_type_t::P1_ACTION1) | - bitmask(input_event_type_t::P1_ACTION2) | - bitmask(input_event_type_t::P1_ACTION3); + bitmask(input_event_type_t::P1_UP) | + bitmask(input_event_type_t::P1_DOWN) | + bitmask(input_event_type_t::P1_RIGHT) | + bitmask(input_event_type_t::P1_LEFT) | + bitmask(input_event_type_t::P1_ACTION1) | + bitmask(input_event_type_t::P1_ACTION2) | + bitmask(input_event_type_t::P1_ACTION3); constexpr static const uint32_t p2_mask = - bitmask(input_event_type_t::P2_UP) | - bitmask(input_event_type_t::P2_DOWN) | - bitmask(input_event_type_t::P2_RIGHT) | - bitmask(input_event_type_t::P2_LEFT) | - bitmask(input_event_type_t::P2_ACTION1) | - bitmask(input_event_type_t::P2_ACTION2) | - bitmask(input_event_type_t::P2_ACTION3); - uint32_t m_pressed; // [P1_UP..RESET] - uint32_t m_lifted; // [P1_UP..RESET] + bitmask(input_event_type_t::P2_UP) | + bitmask(input_event_type_t::P2_DOWN) | + bitmask(input_event_type_t::P2_RIGHT) | + bitmask(input_event_type_t::P2_LEFT) | + bitmask(input_event_type_t::P2_ACTION1) | + bitmask(input_event_type_t::P2_ACTION2) | + bitmask(input_event_type_t::P2_ACTION3); + uint32_t m_pressed; // [P1_UP..RESET] + uint32_t m_lifted; // [P1_UP..RESET] bool m_paused; + public: input_event_type_t last; /** ASCII code, ANY_KEY_UP, ANY_KEY_DOWN key code */ @@ -201,52 +169,52 @@ namespace gamp { pointer_x = x; pointer_y = y; } - void set(input_event_type_t e, uint16_t key_code=0) noexcept { + void set(input_event_type_t e, uint16_t key_code = 0) noexcept { const int bit = bitno(e); - if( 0 <= bit && bit <= 31 ) { + if (0 <= bit && bit <= 31) { const uint32_t m = bitmask(bit); m_lifted &= ~m; m_pressed |= m; } this->last = e; this->last_key_code = key_code; - if( this->text.length() > 0 && '\n' == this->text[this->text.length()-1] ) { + if (this->text.length() > 0 && '\n' == this->text[this->text.length() - 1]) { this->text.clear(); } - if( 0 != key_code && is_ascii_code(key_code) ) { - if( 0x08 == key_code ) { - if( this->text.length() > 0 ) { + if (0 != key_code && is_ascii_code(key_code)) { + if (0x08 == key_code) { + if (this->text.length() > 0) { this->text.pop_back(); } } else { - this->text.push_back( (char)key_code ); + this->text.push_back((char)key_code); } } } - void clear(input_event_type_t e, uint16_t key_code=0) noexcept { + void clear(input_event_type_t e, uint16_t key_code = 0) noexcept { (void)key_code; const int bit = bitno(e); - if( 0 <= bit && bit <= 31 ) { + if (0 <= bit && bit <= 31) { const uint32_t m = bitmask(bit); m_lifted |= m_pressed & m; m_pressed &= ~m; this->last_key_code = 0; } - if( input_event_type_t::PAUSE == e ) { + if (input_event_type_t::PAUSE == e) { m_paused = !m_paused; } } bool paused() const noexcept { return m_paused; } bool pressed(input_event_type_t e) const noexcept { const int bit = bitno(e); - if( 0 <= bit && bit <= 31 ) { - return 0 != ( m_pressed & bitmask(bit) ); + if (0 <= bit && bit <= 31) { + return 0 != (m_pressed & bitmask(bit)); } else { return false; } } bool pressed_and_clr(input_event_type_t e) noexcept { - if( pressed(e) ) { + if (pressed(e)) { clear(e); return true; } else { @@ -255,9 +223,9 @@ namespace gamp { } bool released_and_clr(input_event_type_t e) noexcept { const int bit = bitno(e); - if( 0 <= bit && bit <= 31 ) { + if (0 <= bit && bit <= 31) { const uint32_t m = bitmask(bit); - if( 0 != ( m_lifted & m ) ) { + if (0 != (m_lifted & m)) { m_lifted &= ~m; return true; } @@ -265,10 +233,10 @@ namespace gamp { return false; } bool has_any_p1() const noexcept { - return 0 != ( ( m_pressed | m_lifted ) & p1_mask ); + return 0 != ((m_pressed | m_lifted) & p1_mask); } bool has_any_p2() const noexcept { - return 0 != ( ( m_pressed | m_lifted ) & p2_mask ); + return 0 != ((m_pressed | m_lifted) & p2_mask); } std::string to_string() const noexcept; }; @@ -287,13 +255,12 @@ namespace gamp { inline bool handle_events(input_event_t& event) noexcept { bool one = false; - while( gamp::handle_one_event(event) ) { + while (gamp::handle_one_event(event)) { one = true; // std::cout << "Input " << to_string(event) << std::endl; } return one; - } -} - -#endif /* GAMP_HPP_ */ + } +} // namespace gamp +#endif /* JAU_GAMP_HPP_ */ diff --git a/include/gamp/gamp_types.hpp b/include/gamp/gamp_types.hpp new file mode 100644 index 0000000..3bc313b --- /dev/null +++ b/include/gamp/gamp_types.hpp @@ -0,0 +1,63 @@ +/* + * Author: Sven Gothel <[email protected]> and Svenson Han Gothel + * Copyright (c) 2022-2024 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#ifndef JAU_GAMP_TYPES_HPP_ +#define JAU_GAMP_TYPES_HPP_ + +#include <cinttypes> +#include <cmath> +#include <cstdarg> +#include <cstdint> +#include <limits> +#include <memory> +#include <mutex> +#include <string> +#include <type_traits> +#include <vector> + +#include <jau/cpp_lang_util.hpp> +#include <jau/environment.hpp> +#include <jau/os/os_support.hpp> + +#include <jau/float_math.hpp> +#include <jau/math/vec2f.hpp> +#include <jau/math/vec2i.hpp> +#include <jau/math/vec3f.hpp> +#include <jau/math/vec4f.hpp> +#include <jau/math/mat4f.hpp> +#include <jau/math/recti.hpp> +#include <jau/math/util/pmvmat4f.hpp> + +#if defined(__EMSCRIPTEN__) +#include <emscripten.h> +#else +#define EMSCRIPTEN_KEEPALIVE +#endif + +namespace gamp { + /** A native handle type, big enough to store a pointer. */ + typedef uintptr_t handle_t; + +} // namespace gamp + +#endif /* JAU_GAMP_TYPES_HPP_ */ diff --git a/src/gamp.cpp b/src/gamp.cpp index d41f39b..ca5e49d 100644 --- a/src/gamp.cpp +++ b/src/gamp.cpp @@ -1,6 +1,6 @@ /* - * Author: Sven Gothel <[email protected]> - * Copyright (c) 2022 Gothel Software e.K. + * Author: Sven Gothel <[email protected]> and Svenson Han Gothel + * Copyright (c) 2022-2024 Gothel Software e.K. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/src/sdl_subsys.cpp b/src/sdl_subsys.cpp index 1866e5b..4c18e4f 100644 --- a/src/sdl_subsys.cpp +++ b/src/sdl_subsys.cpp @@ -1,6 +1,6 @@ /* - * Author: Sven Gothel <[email protected]> - * Copyright (c) 2022 Gothel Software e.K. + * Author: Sven Gothel <[email protected]> and Svenson Han Gothel + * Copyright (c) 2022-2024 Gothel Software e.K. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -21,11 +21,14 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "gamp/gamp.hpp" +#include <gamp/gamp.hpp> #include <jau/basic_types.hpp> #include <jau/environment.hpp> #include <jau/fraction_type.hpp> +#include <jau/math/vec2i.hpp> + +#include <cstdint> #include <thread> #include <GLES2/gl2.h> @@ -48,11 +51,11 @@ static jau::fraction_timespec gpu_fps_t0, gpu_swap_t0; jau::math::Recti gamp::viewport; static void on_window_resized(int wwidth, int wheight) noexcept { - int wwidth2=0, wheight2=0; + int wwidth2 = 0, wheight2 = 0; SDL_GetWindowSize(sdl_win, &wwidth2, &wheight2); - + printf("Win Size %d x %d -> %d x %d (given), %d x %d (query)\n", win_width, win_height, wwidth, wheight, wwidth2, wheight2); - if( 0 == wwidth || 0 == wheight ) { + if (0 == wwidth || 0 == wheight) { wwidth = wwidth2; wheight = wheight2; } @@ -62,103 +65,105 @@ static void on_window_resized(int wwidth, int wheight) noexcept { int fb_width = 0; int fb_height = 0; SDL_RendererInfo sdi; - if( nullptr != sdl_rend ) { + if (nullptr != sdl_rend) { SDL_GetRendererOutputSize(sdl_rend, &fb_width, &fb_height); SDL_GetRendererInfo(sdl_rend, &sdi); - printf("SDL Renderer %s\n", sdi.name); + printf("SDL Renderer %s\n", sdi.name); printf("SDL Renderer Size %d x %d\n", fb_width, fb_height); } else { printf("SDL Renderer null\n"); - fb_width = static_cast<int>(static_cast<float>(wwidth) * devicePixelRatio[0]); - fb_height = static_cast<int>(static_cast<float>(wheight) * devicePixelRatio[1]); - printf("DevicePixelRatio Size %f x %f -> %d x %d\n", - devicePixelRatio[0], devicePixelRatio[1], fb_width, fb_height); + fb_width = static_cast<int>(static_cast<float>(wwidth) * devicePixelRatio[0]); + fb_height = static_cast<int>(static_cast<float>(wheight) * devicePixelRatio[1]); + printf("DevicePixelRatio Size %f x %f -> %d x %d\n", + devicePixelRatio[0], devicePixelRatio[1], fb_width, fb_height); } - + glViewport(0, 0, fb_width, fb_height); - viewport.setWidth(fb_width); viewport.setHeight(fb_height); - printf("VP Size %s\n", viewport.toString().c_str()); + viewport.setWidth(fb_width); + viewport.setHeight(fb_height); + printf("VP Size %s\n", viewport.toString().c_str()); { SDL_DisplayMode mode; const int win_display_idx = SDL_GetWindowDisplayIndex(sdl_win); bzero(&mode, sizeof(mode)); - SDL_GetCurrentDisplayMode(win_display_idx, &mode); // SDL_GetWindowDisplayMode(..) fails on some systems (wrong refresh_rate and logical size + SDL_GetCurrentDisplayMode(win_display_idx, &mode); // SDL_GetWindowDisplayMode(..) fails on some systems (wrong refresh_rate and logical size printf("WindowDisplayMode: %d x %d @ %d Hz @ display %d\n", mode.w, mode.h, mode.refresh_rate, win_display_idx); display_frames_per_sec = mode.refresh_rate; - } + } } bool gamp::init_gfx_subsystem(const char* title, int wwidth, int wheight, bool enable_vsync) { - if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { // SDL_INIT_EVERYTHING + if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { // SDL_INIT_EVERYTHING printf("SDL: Error initializing: %s\n", SDL_GetError()); return false; } - if( enable_vsync ) { + if (enable_vsync) { SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"); } // SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(gamp_filter_quality).c_str()); - const Uint32 win_flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL; // | SDL_WINDOW_SHOWN; + const Uint32 win_flags = SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL; // | SDL_WINDOW_SHOWN; - if( 0 != win_width && 0 != win_height ) { + if (0 != win_width && 0 != win_height) { // override using pre-set default, i.e. set_window_size(..) - wwidth = win_width; wheight = win_height; + wwidth = win_width; + wheight = win_height; } sdl_win = SDL_CreateWindow(title, - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - wwidth, wheight, - win_flags); - if( nullptr == sdl_win ) { + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + wwidth, wheight, + win_flags); + if (nullptr == sdl_win) { printf("SDL: Error initializing window: %s\n", SDL_GetError()); return false; } - + sdl_win_id = SDL_GetWindowID(sdl_win); - if( 0 == sdl_win_id ) { + if (0 == sdl_win_id) { printf("SDL: Error retrieving window ID: %s\n", SDL_GetError()); SDL_DestroyWindow(sdl_win); return false; } - + // Create OpenGL ES 3 or ES 2 context on SDL window - SDL_GL_SetSwapInterval( enable_vsync ? 1 : 0 ); + SDL_GL_SetSwapInterval(enable_vsync ? 1 : 0); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); - sdl_glc = SDL_GL_CreateContext(sdl_win); - if( nullptr == sdl_glc ) { + sdl_glc = SDL_GL_CreateContext(sdl_win); + if (nullptr == sdl_glc) { printf("SDL: Error creating GL ES 3 context: %s\n", SDL_GetError()); 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_glc = SDL_GL_CreateContext(sdl_win); - if( nullptr == sdl_glc ) { + if (nullptr == sdl_glc) { printf("SDL: Error creating GL ES 2 context: %s\n", SDL_GetError()); SDL_DestroyWindow(sdl_win); return false; - } - } - if( 0 != SDL_GL_MakeCurrent(sdl_win, sdl_glc) ) { + } + } + if (0 != SDL_GL_MakeCurrent(sdl_win, sdl_glc)) { printf("SDL: Error making GL context current: %s\n", SDL_GetError()); SDL_GL_DeleteContext(sdl_glc); SDL_DestroyWindow(sdl_win); - return false; + return false; } const GLubyte* gl_version = glGetString(GL_VERSION); - if( nullptr == gl_version ) { + if (nullptr == gl_version) { printf("SDL: Error retrieving GL version: %s\n", SDL_GetError()); SDL_GL_DeleteContext(sdl_glc); SDL_DestroyWindow(sdl_win); - return false; + return false; } printf("SDL GL context: %s\n", gl_version); - + // const Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; - sdl_rend = SDL_GetRenderer(sdl_win); // SDL_CreateRenderer(sdl_win, -1, render_flags); - + sdl_rend = SDL_GetRenderer(sdl_win); // SDL_CreateRenderer(sdl_win, -1, render_flags); + gpu_stats_fps = 0.0f; gpu_fps_t0 = jau::getMonotonicTime(); gpu_swap_t0 = gpu_fps_t0; @@ -175,34 +180,33 @@ extern "C" { EMSCRIPTEN_KEEPALIVE void set_window_size(int ww, int wh, float devPixelRatioX, float devPixelRatioY) noexcept { static bool warn_once = true; - if( win_width != ww || win_height != wh || - devicePixelRatio[0] != devPixelRatioX || devicePixelRatio[1] != devPixelRatioY) - { - if( devPixelRatioX >= 0.5f && devPixelRatioY >= 0.5f ) { + if (win_width != ww || win_height != wh || + devicePixelRatio[0] != devPixelRatioX || devicePixelRatio[1] != devPixelRatioY) { + if (devPixelRatioX >= 0.5f && devPixelRatioY >= 0.5f) { devicePixelRatio[0] = devPixelRatioX; devicePixelRatio[1] = devPixelRatioY; } - if( std::abs(win_width - ww) > 1 || std::abs(win_height - wh) > 1 ) { - if( 0 == win_width || 0 == win_height ) { - printf("JS Window Initial Size: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", - win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); + if (std::abs(win_width - ww) > 1 || std::abs(win_height - wh) > 1) { + if (0 == win_width || 0 == win_height) { + printf("JS Window Initial Size: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", + win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); win_width = ww; win_height = wh; } else { - printf("JS Window Resized: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", - win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); - SDL_SetWindowSize( sdl_win, ww, wh ); + printf("JS Window Resized: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", + win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); + SDL_SetWindowSize(sdl_win, ww, wh); warn_once = true; on_window_resized(ww, wh); } - } else if( warn_once ) { + } else if (warn_once) { warn_once = false; - printf("JS Window Resize Ignored: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", - win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); + printf("JS Window Resize Ignored: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", + win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); } } else { - printf("JS Window Resize Same-Size: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", - win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); + printf("JS Window Resize Same-Size: Win %d x %d -> %d x %d, devPixelRatio %f / %f\n", + win_width, win_height, ww, wh, devicePixelRatio[0], devicePixelRatio[1]); } } } @@ -217,40 +221,40 @@ static bool gpu_stats_show = false; void gamp::swap_gpu_buffer(int fps) noexcept { SDL_GL_SwapWindow(sdl_win); - jau::fraction_timespec gpu_swap_t1 = jau::getMonotonicTime(); + jau::fraction_timespec gpu_swap_t1 = jau::getMonotonicTime(); const jau::fraction_timespec td_last_frame = gpu_swap_t1 - gpu_swap_t0; td_net_costs += td_last_frame; ++gpu_stats_frame_count; const jau::fraction_timespec td = gpu_swap_t1 - gpu_fps_t0; - if( td >= gpu_stats_period ) { + if (td >= gpu_stats_period) { const double gpu_frame_count_d = gpu_stats_frame_count; - gpu_stats_fps = (float)gpu_stats_frame_count / ( (float)td.tv_sec + ( (float)td.tv_nsec / 1000000000.0f ) ); - gpu_stats_frame_costs_in_sec = ( (double)td_net_costs.tv_sec + ( (double)td_net_costs.tv_nsec / 1000000000.0f ) ) / gpu_frame_count_d; - gpu_stats_frame_slept_in_sec = ( (double)td_slept.tv_sec + ( (double)td_slept.tv_nsec / 1000000000.0f ) ) / gpu_frame_count_d; - if( gpu_stats_show ) { - jau::fprintf_td(gpu_swap_t1.to_ms(), stdout, "fps: %f (req %d), frames %d, td %s, costs %fms/frame, slept %fms/frame\n", - gpu_stats_fps, fps, gpu_stats_frame_count, td.to_string().c_str(), gpu_stats_frame_costs_in_sec*1000.0, gpu_stats_frame_slept_in_sec*1000.0); + gpu_stats_fps = (float)gpu_stats_frame_count / ((float)td.tv_sec + ((float)td.tv_nsec / 1000000000.0f)); + gpu_stats_frame_costs_in_sec = ((double)td_net_costs.tv_sec + ((double)td_net_costs.tv_nsec / 1000000000.0f)) / gpu_frame_count_d; + gpu_stats_frame_slept_in_sec = ((double)td_slept.tv_sec + ((double)td_slept.tv_nsec / 1000000000.0f)) / gpu_frame_count_d; + if (gpu_stats_show) { + jau::fprintf_td(gpu_swap_t1.to_ms(), stdout, "fps: %f (req %d), frames %d, td %s, costs %fms/frame, slept %fms/frame\n", + gpu_stats_fps, fps, gpu_stats_frame_count, td.to_string().c_str(), gpu_stats_frame_costs_in_sec * 1000.0, gpu_stats_frame_slept_in_sec * 1000.0); } gpu_fps_t0 = gpu_swap_t1; gpu_stats_frame_count = 0; td_net_costs = 0_s; td_slept = 0_s; } - if( 0 < fps ) { + if (0 < fps) { const jau::fraction_timespec fudge(1_ms / 4_i64); // const int64_t fudge_ns = gamp::NanoPerMilli / 4; // const uint64_t ms_per_frame = (uint64_t)std::round(1000.0 / fps); const jau::fraction_timespec td_per_frame(1_s / (int64_t)fps); const jau::fraction_timespec tdd = td_per_frame - td_last_frame; - if( tdd > fudge ) { - jau::sleep( tdd, false ); // allow IRQ + if (tdd > fudge) { + jau::sleep(tdd, false); // allow IRQ td_slept += tdd; // gpu_swap_t0 += tdd; // accurate enough? gpu_swap_t0 = jau::getMonotonicTime(); // jau::fprintf_td(gpu_swap_t0.to_ms(), stdout, "soft-sync.1 [exp %" PRIi64 " > has %" PRIi64 "]us, delay %" PRIi64 "us\n", // td_per_frame.to_us(), td_this_frame.to_us(), tdd.to_us()); } else { - gpu_swap_t0 = gpu_swap_t1; + gpu_swap_t0 = gpu_swap_t1; } } else { gpu_swap_t0 = jau::getMonotonicTime(); @@ -262,7 +266,7 @@ float gamp::get_gpu_stats_fps() noexcept { } double gamp::get_gpu_stats_frame_costs() noexcept { - return gpu_stats_frame_costs_in_sec; + return gpu_stats_frame_costs_in_sec; } double gamp::get_gpu_stats_frame_sleep() noexcept { return gpu_stats_frame_slept_in_sec; @@ -279,7 +283,7 @@ void gamp::set_gpu_stats_show(bool enable) noexcept { } static input_event_type_t to_event_type(SDL_Scancode scancode) { - switch ( scancode ) { + switch (scancode) { case SDL_SCANCODE_ESCAPE: return input_event_type_t::WINDOW_CLOSE_REQ; case SDL_SCANCODE_P: @@ -325,57 +329,70 @@ static input_event_type_t to_event_type(SDL_Scancode scancode) { } } static uint16_t to_ascii(SDL_Scancode scancode) { - if(SDL_SCANCODE_A <= scancode && scancode <= SDL_SCANCODE_Z ) { - return 'a' + ( scancode - SDL_SCANCODE_A ); + if (SDL_SCANCODE_A <= scancode && scancode <= SDL_SCANCODE_Z) { + return 'a' + (scancode - SDL_SCANCODE_A); } - if(SDL_SCANCODE_1 <= scancode && scancode <= SDL_SCANCODE_9 ) { - return '1' + ( scancode - SDL_SCANCODE_1 ); + if (SDL_SCANCODE_1 <= scancode && scancode <= SDL_SCANCODE_9) { + return '1' + (scancode - SDL_SCANCODE_1); } - if(SDL_SCANCODE_0 == scancode ) { - return '0' + ( scancode - SDL_SCANCODE_0 ); + if (SDL_SCANCODE_0 == scancode) { + return '0' + (scancode - SDL_SCANCODE_0); } - switch( scancode ) { - case SDL_SCANCODE_SEMICOLON: return ';'; + switch (scancode) { + case SDL_SCANCODE_SEMICOLON: + return ';'; case SDL_SCANCODE_MINUS: [[fallthrough]]; - case SDL_SCANCODE_KP_MINUS: return '-'; + case SDL_SCANCODE_KP_MINUS: + return '-'; - case SDL_SCANCODE_KP_PLUS: return '+'; + case SDL_SCANCODE_KP_PLUS: + return '+'; - case SDL_SCANCODE_KP_MULTIPLY: return '*'; + case SDL_SCANCODE_KP_MULTIPLY: + return '*'; case SDL_SCANCODE_SLASH: [[fallthrough]]; - case SDL_SCANCODE_KP_DIVIDE: return '/'; + case SDL_SCANCODE_KP_DIVIDE: + return '/'; - case SDL_SCANCODE_KP_PERCENT: return '%'; + case SDL_SCANCODE_KP_PERCENT: + return '%'; case SDL_SCANCODE_KP_LEFTPAREN: [[fallthrough]]; case SDL_SCANCODE_KP_LEFTBRACE: [[fallthrough]]; - case SDL_SCANCODE_LEFTBRACKET: return '('; + case SDL_SCANCODE_LEFTBRACKET: + return '('; case SDL_SCANCODE_KP_RIGHTPAREN: [[fallthrough]]; case SDL_SCANCODE_KP_RIGHTBRACE: [[fallthrough]]; - case SDL_SCANCODE_RIGHTBRACKET: return ')'; + case SDL_SCANCODE_RIGHTBRACKET: + return ')'; - case SDL_SCANCODE_COMMA: return ','; + case SDL_SCANCODE_COMMA: + return ','; - case SDL_SCANCODE_PERIOD: return '.'; + case SDL_SCANCODE_PERIOD: + return '.'; case SDL_SCANCODE_SPACE: [[fallthrough]]; - case SDL_SCANCODE_TAB: return ' '; + case SDL_SCANCODE_TAB: + return ' '; case SDL_SCANCODE_RETURN: [[fallthrough]]; - case SDL_SCANCODE_KP_ENTER: return '\n'; + case SDL_SCANCODE_KP_ENTER: + return '\n'; - case SDL_SCANCODE_BACKSPACE: return 0x08; + case SDL_SCANCODE_BACKSPACE: + return 0x08; default: return 0; @@ -386,10 +403,10 @@ static uint16_t to_ascii(SDL_Scancode scancode) { bool gamp::handle_one_event(input_event_t& event) noexcept { SDL_Event sdl_event; - if( SDL_PollEvent(&sdl_event) ) { + if (SDL_PollEvent(&sdl_event)) { switch (sdl_event.type) { case SDL_QUIT: - event.set( input_event_type_t::WINDOW_CLOSE_REQ ); + event.set(input_event_type_t::WINDOW_CLOSE_REQ); printf("Window Close Requested\n"); break; @@ -412,26 +429,23 @@ bool gamp::handle_one_event(input_event_t& event) noexcept { } break; - case SDL_MOUSEMOTION: - event.pointer_motion((int)sdl_event.motion.which, - (int)sdl_event.motion.x, (int)sdl_event.motion.y); - break; - case SDL_KEYUP: { - const SDL_Scancode scancode = sdl_event.key.keysym.scancode; - event.clear(to_event_type(scancode), to_ascii(scancode)); - } - break; - - case SDL_KEYDOWN: { - const SDL_Scancode scancode = sdl_event.key.keysym.scancode; - event.set(to_event_type(scancode), to_ascii(scancode)); - // printf("%d", scancode); - } - break; + case SDL_MOUSEMOTION: + event.pointer_motion((int)sdl_event.motion.which, + (int)sdl_event.motion.x, (int)sdl_event.motion.y); + break; + case SDL_KEYUP: { + const SDL_Scancode scancode = sdl_event.key.keysym.scancode; + event.clear(to_event_type(scancode), to_ascii(scancode)); + } break; + + case SDL_KEYDOWN: { + const SDL_Scancode scancode = sdl_event.key.keysym.scancode; + event.set(to_event_type(scancode), to_ascii(scancode)); + // printf("%d", scancode); + } break; } return true; } else { return false; } } - diff --git a/test/placeholder.txt b/test/placeholder.txt new file mode 100644 index 0000000..a099036 --- /dev/null +++ b/test/placeholder.txt @@ -0,0 +1 @@ +TBD |