Tue, 16 Apr 2024 22:20:17 +0200
implement mouse motion and key press events
src/ascension/context.h | file | annotate | diff | comparison | revisions | |
src/ascension/input.h | file | annotate | diff | comparison | revisions | |
src/ascension/scene.h | file | annotate | diff | comparison | revisions | |
src/context.c | file | annotate | diff | comparison | revisions | |
test/snake.c | file | annotate | diff | comparison | revisions |
--- a/src/ascension/context.h Tue Apr 16 22:06:17 2024 +0200 +++ b/src/ascension/context.h Tue Apr 16 22:20:17 2024 +0200 @@ -30,6 +30,7 @@ #include "datatypes.h" #include "window.h" +#include "input.h" #include "ui/font.h" #include <cx/buffer.h> @@ -49,6 +50,7 @@ typedef struct AscContext { unsigned int flags; CxBuffer error_buffer; + AscInput input; AscWindow windows[ASC_MAX_WINDOWS]; AscWindow *active_window; AscFont fonts[ASC_MAX_FONTS]; @@ -65,6 +67,10 @@ void asc_context_initialize(void); void asc_context_destroy(void); +/** + * Signals the context that we want to quit the application. + */ +void asc_context_quit(void); /** * Dispatches events and synchronizes all initialized windows.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ascension/input.h Tue Apr 16 22:20:17 2024 +0200 @@ -0,0 +1,48 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2024 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ASCENSION_INPUT_H +#define ASCENSION_INPUT_H + +#include "window.h" + +#include <SDL2/SDL_scancode.h> + +typedef struct AscInput AscInput; + +struct AscInput { + int mouse_x; + int mouse_y; + int mouse_xrel; + int mouse_yrel; + bool keys[SDL_NUM_SCANCODES]; +}; + +#define asc_key_pressed(scancode) \ + (asc_context.input.keys[SDL_SCANCODE_##scancode]) + +#endif //ASCENSION_INPUT_H
--- a/src/ascension/scene.h Tue Apr 16 22:06:17 2024 +0200 +++ b/src/ascension/scene.h Tue Apr 16 22:20:17 2024 +0200 @@ -55,7 +55,7 @@ asc_scene_free_func free_func; asc_scene_update_func update_func; asc_scene_draw_func draw_func; - unsigned depth; + unsigned depth; // TODO: do we really need this bullshit? asc_vec3f position; asc_vec3f rotation; asc_vec3f scale;
--- a/src/context.c Tue Apr 16 22:06:17 2024 +0200 +++ b/src/context.c Tue Apr 16 22:20:17 2024 +0200 @@ -87,6 +87,10 @@ asc_dprintf("Ascension context destroyed."); } +void asc_context_quit(void) { + asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); +} + static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { if (asc_context.windows[i].id == id) { @@ -99,6 +103,10 @@ } bool asc_loop_next(void) { + // reset mouse motion + asc_context.input.mouse_xrel = 0; + asc_context.input.mouse_yrel = 0; + // dispatch SDL events SDL_Event event; while (SDL_PollEvent(&event)) { @@ -115,13 +123,20 @@ ); break; } + case SDL_MOUSEMOTION: { + // accumulate relative motion + asc_context.input.mouse_xrel += event.motion.xrel; + asc_context.input.mouse_yrel += event.motion.yrel; + // update absolute position + asc_context.input.mouse_x = event.motion.x; + asc_context.input.mouse_y = event.motion.y; + break; + } case SDL_KEYDOWN: - // TODO: remove this code and implement key press map instead - if (event.key.keysym.sym == SDLK_ESCAPE) - return false; + asc_context.input.keys[event.key.keysym.scancode] = true; break; case SDL_KEYUP: - // TODO: implement key press map + asc_context.input.keys[event.key.keysym.scancode] = false; break; } }
--- a/test/snake.c Tue Apr 16 22:06:17 2024 +0200 +++ b/test/snake.c Tue Apr 16 22:20:17 2024 +0200 @@ -100,7 +100,12 @@ SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", asc_get_error(), window->window); asc_clear_error(); - break; + asc_context_quit(); + } + + // quit application on ESC key press + if (asc_key_pressed(ESCAPE)) { + asc_context_quit(); } } while (asc_loop_next());