Add mouse stuff

This commit is contained in:
Fierelier 2023-09-25 17:39:50 +02:00
parent ad5c4b9729
commit 5b99ad3be9
7 changed files with 81 additions and 16 deletions

View File

@ -8,6 +8,7 @@ end
local width = 320
local height = 240
local framelimit = 60
local mouseSensitivity = 0.5
local fovVert = 67
local fovHor = fovVert * (width / height)
framelimit = math.floor(1000 / framelimit)
@ -38,13 +39,16 @@ function main()
loadchar("question","?")
engine_rendertarget_set(engine_window_texture_get(window))
-- Capture mouse
engine_input_mouse_trap(1)
-- Clear window
engine_color_set(0,0,0,255)
engine_rendertarget_fill(0,0,width - 1,height - 1)
while true do
event = engine_event_get()
eventData = {engine_lua_event_get_data(event)}
local event = engine_event_get()
local eventData = {engine_lua_event_get_data(event)}
engine_event_free(event)
if eventData[1] ~= ENGINE_EVENT_TYPE_NONE then
handleEvent(event,eventData)
@ -58,6 +62,10 @@ function logic()
engine_color_set(0,0,20,255)
engine_rendertarget_fill(0,0,width - 1,height - 1)
if pressedKeys[41] then -- ESC
os.exit()
end
if pressedKeys[26] then -- w
prx = prx + (0.1 * frametime)
end
@ -159,8 +167,13 @@ function tick()
lastFrame = engine_time_get()
end
function rotateCamera(x,y)
prx = prx - (y * mouseSensitivity)
pry = pry + (x * mouseSensitivity)
end
pressedKeys = {}
function handleEvent()
function handleEvent(event,eventData)
if eventData[1] == ENGINE_EVENT_TYPE_EXIT then
os.exit()
end
@ -174,6 +187,10 @@ function handleEvent()
pressedKeys[eventData[2]] = nil
end
end
if eventData[1] == ENGINE_EVENT_TYPE_INPUT_MOUSE_MOVE then
rotateCamera(eventData[4],eventData[5])
end
end
framerate = 0

View File

@ -36,8 +36,12 @@ static char ENGINE_EVENT_TYPE_EXIT = 2;
struct ENGINE_EVENT_EXIT { };
static char ENGINE_EVENT_TYPE_INPUT_KB = 3;
struct ENGINE_EVENT_INPUT_KB { char key; char pressed; };
static char ENGINE_EVENT_TYPE_INPUT_MOUSE_MOVE = 4;
struct ENGINE_EVENT_INPUT_MOUSE_MOVE { int x; int y; int relX; int relY; };
static char ENGINE_EVENT_TYPE_INPUT_MOUSE_PRESS = 5;
struct ENGINE_EVENT_INPUT_MOUSE_PRESS { int button; char pressed; };
void engine_event_free(struct ENGINE_EVENT * event) {
engine_memory_free(event->data);
engine_memory_free(event);
}
}

View File

@ -1,7 +1,7 @@
// This is the SDL2 software frontend.
#include <SDL2/SDL.h>
// EVENTS
// EVENT
struct ENGINE_EVENT * engine_event_get() {
struct ENGINE_EVENT * event = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT));
SDL_Event sdlevent;
@ -30,6 +30,17 @@ struct ENGINE_EVENT * engine_event_get() {
return event;
}
if (sdlevent.type == SDL_MOUSEMOTION) {
struct ENGINE_EVENT_INPUT_MOUSE_MOVE * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_INPUT_MOUSE_MOVE));
data->x = sdlevent.motion.x;
data->y = sdlevent.motion.y;
data->relX = sdlevent.motion.xrel;
data->relY = sdlevent.motion.yrel;
event->type = ENGINE_EVENT_TYPE_INPUT_MOUSE_MOVE;
event->data = data;
return event;
}
unknown: ;
struct ENGINE_EVENT_UNKNOWN * data = engine_memory_alloc(NULL,sizeof(struct ENGINE_EVENT_UNKNOWN));
event->type = ENGINE_EVENT_TYPE_UNKNOWN;
@ -37,6 +48,15 @@ struct ENGINE_EVENT * engine_event_get() {
return event;
}
// INPUT
void engine_input_mouse_trap(int toggle) {
SDL_bool toggle_bool = SDL_FALSE;
if (toggle == 1) {
toggle_bool = SDL_TRUE;
}
SDL_SetRelativeMouseMode(toggle_bool);
}
// TEXTURE
struct ENGINE_TEXTURE * engine_texture_create(int width,int height) {
struct ENGINE_TEXTURE * texture = engine_memory_alloc(NULL,sizeof(struct ENGINE_TEXTURE));
@ -94,15 +114,15 @@ void engine_rendertarget_set(struct ENGINE_TEXTURE * texture) {
}
}
int engine_frontend_skip_fraction = 9;
int engine_frontend_skip_status = 0;
//int engine_frontend_skip_fraction = 9;
//int engine_frontend_skip_status = 0;
void engine_rendertarget_draw_point(int x,int y) {
if (engine_rendertarget->fe_texture->surface != NULL) {
Uint32 pixelIndex = x + y * engine_rendertarget -> width;
if ((pixelIndex) % engine_frontend_skip_fraction != engine_frontend_skip_status) {
return;
}
//if ((pixelIndex) % engine_frontend_skip_fraction != engine_frontend_skip_status) {
// return;
//}
Uint32 * pixels = engine_rendertarget->fe_texture->surface->pixels;
pixels[pixelIndex] = engine_frontend_fbcolor;
@ -138,7 +158,7 @@ struct ENGINE_WINDOW * engine_window_create(int width,int height,char * title) {
}
void engine_window_present(struct ENGINE_WINDOW * window) {
if (++engine_frontend_skip_status == engine_frontend_skip_fraction) { engine_frontend_skip_status = 0; }
//if (++engine_frontend_skip_status == engine_frontend_skip_fraction) { engine_frontend_skip_status = 0; }
SDL_UpdateWindowSurface(window->fe_window->window);
}
@ -146,4 +166,4 @@ void engine_window_destroy(struct ENGINE_WINDOW * window) {
engine_memory_free(window -> texture);
engine_memory_free(window -> fe_window);
engine_memory_free(window);
}
}

View File

@ -12,6 +12,16 @@ int engine_luaf_lua_event_get_data(lua_State *L) {
return 3;
}
if (type == ENGINE_EVENT_TYPE_INPUT_MOUSE_MOVE) {
struct ENGINE_EVENT_INPUT_MOUSE_MOVE * data = event->data;
lua_pushinteger(L,type);
lua_pushinteger(L,data->x);
lua_pushinteger(L,data->y);
lua_pushinteger(L,data->relX);
lua_pushinteger(L,data->relY);
return 5;
}
type = ENGINE_EVENT_TYPE_UNKNOWN;
simple:
lua_pushinteger(L,type);

View File

@ -1,12 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include "frontend/sdl/structs.c"
//#include "frontend/sdl/structs.c"
#include "engine.c"
#include "frontend/sdl/main.c"
#include "helpers.c"
#include "3d.c"
//#include "frontend/sdl/main.c"
#include "lua.c"
int main(int argc, char **argv) {

View File

@ -12,7 +12,7 @@ arguments = ["void *"]
argNames = ["ptr"]
description = "Free memory, equivalent to C's free()."
# EVENTS
# EVENT
[engine_event_get]
type = "struct ENGINE_EVENT *"
arguments = []
@ -25,7 +25,13 @@ arguments = ["struct ENGINE_EVENT *"]
argNames = ["event"]
description = "Free an event you have handled from memory."
# TEXTURES
# INPUT
[engine_input_mouse_trap]
type = "void"
arguments = ["char"]
argNames = ["toggle"]
# TEXTURE
[engine_texture_create]
type = "struct ENGINE_TEXTURE *"
arguments = ["int", "int"]

View File

@ -17,3 +17,13 @@ description = "User has requested the application to exit."
type = "char"
value = 3
description = "A keyboard input has occured."
[ENGINE_EVENT_TYPE_INPUT_MOUSE_MOVE]
type = "char"
value = 4
description = "The mouse has been moved."
[ENGINE_EVENT_TYPE_INPUT_MOUSE_PRESS]
type = "char"
value = 5
description = "The mouse has been clicked."