From 2f6c88f4358b12270d41951063af4311fccb7352 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Wed, 6 Feb 2019 10:32:31 +0100 Subject: [PATCH] Added crude input API between platform layer and plugin. Added manual refresh to prevent CPU from blowing up. Added last rendered value to window title. --- code/rt_weekend.cpp | 3 ++- code/rt_weekend.h | 7 ++++++ code/sdl_platform.cpp | 55 +++++++++++++++++++++++++++++++++++------ code/sdl_platform.h | 2 +- code/win32_platform.cpp | 2 +- code/win32_platform.h | 2 +- 6 files changed, 59 insertions(+), 12 deletions(-) diff --git a/code/rt_weekend.cpp b/code/rt_weekend.cpp index 0ebb324..a0e848b 100644 --- a/code/rt_weekend.cpp +++ b/code/rt_weekend.cpp @@ -3,6 +3,7 @@ #include "sphere.h" #include "target_list.h" #include "camera.h" +#include vec3 color(const ray &r, target *world) { @@ -21,7 +22,7 @@ vec3 color(const ray &r, target *world) extern "C" void -PluginUpdateAndRender(plugin_offscreen_buffer *Buffer) +PluginUpdateAndRender(plugin_offscreen_buffer *Buffer, plugin_input *Input) { int ns = 1; int Pitch = Buffer->Pitch; diff --git a/code/rt_weekend.h b/code/rt_weekend.h index bd29024..2f19856 100644 --- a/code/rt_weekend.h +++ b/code/rt_weekend.h @@ -19,4 +19,11 @@ struct plugin_offscreen_buffer int Pitch; }; +struct plugin_input +{ + uint32_t MouseX; + uint32_t MouseY; + bool MouseDown; +}; + #endif diff --git a/code/sdl_platform.cpp b/code/sdl_platform.cpp index f54aa01..6a60151 100644 --- a/code/sdl_platform.cpp +++ b/code/sdl_platform.cpp @@ -14,13 +14,13 @@ const char *WindowTitle = "Ray Tracing in a Weekend"; const uint32_t TARGET_FRAME_RATE = 10; const uint32_t TICKS_PER_FRAME = 1000 / TARGET_FRAME_RATE; -const uint32_t WINDOW_WIDTH = 500; -const uint32_t WINDOW_HEIGHT = 250; +const uint32_t WINDOW_WIDTH = 1000; +const uint32_t WINDOW_HEIGHT = 500; global_variable bool Running = true; global_variable sdl_offscreen_buffer GlobalBackbuffer; -void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer) +void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer, plugin_input *Input) { } @@ -79,7 +79,7 @@ SDLUpdateWindow(SDL_Renderer *Renderer, } internal void -HandleEvent(SDL_Event *Event) +HandleEvent(SDL_Event *Event, plugin_input *Input) { switch(Event->type) { @@ -100,6 +100,32 @@ HandleEvent(SDL_Event *Event) } break; } } + + case SDL_MOUSEMOTION: + { + // printf("We got a motion event.\n"); + // printf("Current mouse position is: (%d, %d)\n", + // Event->motion.x, + // Event->motion.y); + Input->MouseX = Event->motion.x; + Input->MouseY = Event->motion.y; + + } break; + + case SDL_MOUSEBUTTONDOWN: + { + Input->MouseDown = true; + } break; + + case SDL_MOUSEBUTTONUP: + { + Input->MouseDown = false; + } break; + + default: + { + // printf("Unhandled event!\n"); + } break; } } @@ -217,6 +243,12 @@ int main() const char *PluginLibraryFilename = "librt_weekend.so"; sdl_plugin_code Plugin = SDLLoadPluginCode(PluginLibraryFilename); + // Keep track of input + plugin_input Input = {}; + + bool FirstFrame = true; + uint32_t LastRenderedTime = 0; + // Main loop while (Running) { @@ -233,18 +265,19 @@ int main() SDL_Event Event; while (SDL_PollEvent(&Event)) { - HandleEvent(&Event); + HandleEvent(&Event, &Input); } // Display rendering information in Window title char WindowTitleBuffer[512]; sprintf(WindowTitleBuffer, - "%s - %d x %d - %u fps - %d frames missed", + "%s - %d x %d - %u fps - %d frames missed - Last rendered: %d", WindowTitle, WINDOW_WIDTH, WINDOW_HEIGHT, CurrentFPS, - FramesMissed); + FramesMissed, + LastRenderedTime); SDL_SetWindowTitle(Window, WindowTitleBuffer); @@ -253,7 +286,13 @@ int main() Buffer.Width = GlobalBackbuffer.Width; Buffer.Height = GlobalBackbuffer.Height; Buffer.Pitch = GlobalBackbuffer.Pitch; - Plugin.UpdateAndRender(&Buffer); + + if (FirstFrame || Input.MouseDown) + { + LastRenderedTime = SDL_GetTicks(); + Plugin.UpdateAndRender(&Buffer, &Input); + } + FirstFrame = false; SDLUpdateWindow(Renderer, GlobalBackbuffer); diff --git a/code/sdl_platform.h b/code/sdl_platform.h index ec4b09f..be5b796 100644 --- a/code/sdl_platform.h +++ b/code/sdl_platform.h @@ -1,7 +1,7 @@ #ifndef SDL_PLATFORM_H #define SDL_PLATFORM_H -typedef void plugin_update_and_render(plugin_offscreen_buffer *Buffer); +typedef void plugin_update_and_render(plugin_offscreen_buffer *Buffer, plugin_input *Input); struct sdl_plugin_code { diff --git a/code/win32_platform.cpp b/code/win32_platform.cpp index 4a00219..852098f 100644 --- a/code/win32_platform.cpp +++ b/code/win32_platform.cpp @@ -20,7 +20,7 @@ const uint32_t WINDOW_HEIGHT = 250; global_variable bool Running = true; global_variable sdl_offscreen_buffer GlobalBackbuffer; -void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer) +void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer, plugin_input *Input) { } diff --git a/code/win32_platform.h b/code/win32_platform.h index 6f3f359..9a971bd 100644 --- a/code/win32_platform.h +++ b/code/win32_platform.h @@ -1,7 +1,7 @@ #ifndef WIN32_PLATFORM_H #define WIN32_PLATFORM_H -typedef void plugin_update_and_render(plugin_offscreen_buffer *Buffer); +typedef void plugin_update_and_render(plugin_offscreen_buffer *Buffer, plugin_input *Input); struct sdl_plugin_code {