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.

This commit is contained in:
Michael Smith 2019-02-06 10:32:31 +01:00
parent 0e5ce7ec07
commit 2f6c88f435
6 changed files with 59 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include "sphere.h" #include "sphere.h"
#include "target_list.h" #include "target_list.h"
#include "camera.h" #include "camera.h"
#include <stdio.h>
vec3 color(const ray &r, target *world) vec3 color(const ray &r, target *world)
{ {
@ -21,7 +22,7 @@ vec3 color(const ray &r, target *world)
extern "C" extern "C"
void void
PluginUpdateAndRender(plugin_offscreen_buffer *Buffer) PluginUpdateAndRender(plugin_offscreen_buffer *Buffer, plugin_input *Input)
{ {
int ns = 1; int ns = 1;
int Pitch = Buffer->Pitch; int Pitch = Buffer->Pitch;

View File

@ -19,4 +19,11 @@ struct plugin_offscreen_buffer
int Pitch; int Pitch;
}; };
struct plugin_input
{
uint32_t MouseX;
uint32_t MouseY;
bool MouseDown;
};
#endif #endif

View File

@ -14,13 +14,13 @@ const char *WindowTitle = "Ray Tracing in a Weekend";
const uint32_t TARGET_FRAME_RATE = 10; const uint32_t TARGET_FRAME_RATE = 10;
const uint32_t TICKS_PER_FRAME = 1000 / TARGET_FRAME_RATE; const uint32_t TICKS_PER_FRAME = 1000 / TARGET_FRAME_RATE;
const uint32_t WINDOW_WIDTH = 500; const uint32_t WINDOW_WIDTH = 1000;
const uint32_t WINDOW_HEIGHT = 250; const uint32_t WINDOW_HEIGHT = 500;
global_variable bool Running = true; global_variable bool Running = true;
global_variable sdl_offscreen_buffer GlobalBackbuffer; 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 internal void
HandleEvent(SDL_Event *Event) HandleEvent(SDL_Event *Event, plugin_input *Input)
{ {
switch(Event->type) switch(Event->type)
{ {
@ -100,6 +100,32 @@ HandleEvent(SDL_Event *Event)
} break; } 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"; const char *PluginLibraryFilename = "librt_weekend.so";
sdl_plugin_code Plugin = SDLLoadPluginCode(PluginLibraryFilename); sdl_plugin_code Plugin = SDLLoadPluginCode(PluginLibraryFilename);
// Keep track of input
plugin_input Input = {};
bool FirstFrame = true;
uint32_t LastRenderedTime = 0;
// Main loop // Main loop
while (Running) while (Running)
{ {
@ -233,18 +265,19 @@ int main()
SDL_Event Event; SDL_Event Event;
while (SDL_PollEvent(&Event)) while (SDL_PollEvent(&Event))
{ {
HandleEvent(&Event); HandleEvent(&Event, &Input);
} }
// Display rendering information in Window title // Display rendering information in Window title
char WindowTitleBuffer[512]; char WindowTitleBuffer[512];
sprintf(WindowTitleBuffer, sprintf(WindowTitleBuffer,
"%s - %d x %d - %u fps - %d frames missed", "%s - %d x %d - %u fps - %d frames missed - Last rendered: %d",
WindowTitle, WindowTitle,
WINDOW_WIDTH, WINDOW_WIDTH,
WINDOW_HEIGHT, WINDOW_HEIGHT,
CurrentFPS, CurrentFPS,
FramesMissed); FramesMissed,
LastRenderedTime);
SDL_SetWindowTitle(Window, WindowTitleBuffer); SDL_SetWindowTitle(Window, WindowTitleBuffer);
@ -253,7 +286,13 @@ int main()
Buffer.Width = GlobalBackbuffer.Width; Buffer.Width = GlobalBackbuffer.Width;
Buffer.Height = GlobalBackbuffer.Height; Buffer.Height = GlobalBackbuffer.Height;
Buffer.Pitch = GlobalBackbuffer.Pitch; Buffer.Pitch = GlobalBackbuffer.Pitch;
Plugin.UpdateAndRender(&Buffer);
if (FirstFrame || Input.MouseDown)
{
LastRenderedTime = SDL_GetTicks();
Plugin.UpdateAndRender(&Buffer, &Input);
}
FirstFrame = false;
SDLUpdateWindow(Renderer, GlobalBackbuffer); SDLUpdateWindow(Renderer, GlobalBackbuffer);

View File

@ -1,7 +1,7 @@
#ifndef SDL_PLATFORM_H #ifndef SDL_PLATFORM_H
#define 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 struct sdl_plugin_code
{ {

View File

@ -20,7 +20,7 @@ const uint32_t WINDOW_HEIGHT = 250;
global_variable bool Running = true; global_variable bool Running = true;
global_variable sdl_offscreen_buffer GlobalBackbuffer; global_variable sdl_offscreen_buffer GlobalBackbuffer;
void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer) void PluginUpdateAndRenderStub(plugin_offscreen_buffer *Buffer, plugin_input *Input)
{ {
} }

View File

@ -1,7 +1,7 @@
#ifndef WIN32_PLATFORM_H #ifndef WIN32_PLATFORM_H
#define 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 struct sdl_plugin_code
{ {