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 |
1.1 --- a/src/ascension/context.h Tue Apr 16 22:06:17 2024 +0200 1.2 +++ b/src/ascension/context.h Tue Apr 16 22:20:17 2024 +0200 1.3 @@ -30,6 +30,7 @@ 1.4 1.5 #include "datatypes.h" 1.6 #include "window.h" 1.7 +#include "input.h" 1.8 #include "ui/font.h" 1.9 1.10 #include <cx/buffer.h> 1.11 @@ -49,6 +50,7 @@ 1.12 typedef struct AscContext { 1.13 unsigned int flags; 1.14 CxBuffer error_buffer; 1.15 + AscInput input; 1.16 AscWindow windows[ASC_MAX_WINDOWS]; 1.17 AscWindow *active_window; 1.18 AscFont fonts[ASC_MAX_FONTS]; 1.19 @@ -65,6 +67,10 @@ 1.20 void asc_context_initialize(void); 1.21 void asc_context_destroy(void); 1.22 1.23 +/** 1.24 + * Signals the context that we want to quit the application. 1.25 + */ 1.26 +void asc_context_quit(void); 1.27 1.28 /** 1.29 * Dispatches events and synchronizes all initialized windows.
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/ascension/input.h Tue Apr 16 22:20:17 2024 +0200 2.3 @@ -0,0 +1,48 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * Copyright 2024 Mike Becker. All rights reserved. 2.7 + * 2.8 + * Redistribution and use in source and binary forms, with or without 2.9 + * modification, are permitted provided that the following conditions are met: 2.10 + * 2.11 + * 1. Redistributions of source code must retain the above copyright 2.12 + * notice, this list of conditions and the following disclaimer. 2.13 + * 2.14 + * 2. Redistributions in binary form must reproduce the above copyright 2.15 + * notice, this list of conditions and the following disclaimer in the 2.16 + * documentation and/or other materials provided with the distribution. 2.17 + * 2.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.28 + * POSSIBILITY OF SUCH DAMAGE. 2.29 + */ 2.30 + 2.31 +#ifndef ASCENSION_INPUT_H 2.32 +#define ASCENSION_INPUT_H 2.33 + 2.34 +#include "window.h" 2.35 + 2.36 +#include <SDL2/SDL_scancode.h> 2.37 + 2.38 +typedef struct AscInput AscInput; 2.39 + 2.40 +struct AscInput { 2.41 + int mouse_x; 2.42 + int mouse_y; 2.43 + int mouse_xrel; 2.44 + int mouse_yrel; 2.45 + bool keys[SDL_NUM_SCANCODES]; 2.46 +}; 2.47 + 2.48 +#define asc_key_pressed(scancode) \ 2.49 + (asc_context.input.keys[SDL_SCANCODE_##scancode]) 2.50 + 2.51 +#endif //ASCENSION_INPUT_H
3.1 --- a/src/ascension/scene.h Tue Apr 16 22:06:17 2024 +0200 3.2 +++ b/src/ascension/scene.h Tue Apr 16 22:20:17 2024 +0200 3.3 @@ -55,7 +55,7 @@ 3.4 asc_scene_free_func free_func; 3.5 asc_scene_update_func update_func; 3.6 asc_scene_draw_func draw_func; 3.7 - unsigned depth; 3.8 + unsigned depth; // TODO: do we really need this bullshit? 3.9 asc_vec3f position; 3.10 asc_vec3f rotation; 3.11 asc_vec3f scale;
4.1 --- a/src/context.c Tue Apr 16 22:06:17 2024 +0200 4.2 +++ b/src/context.c Tue Apr 16 22:20:17 2024 +0200 4.3 @@ -87,6 +87,10 @@ 4.4 asc_dprintf("Ascension context destroyed."); 4.5 } 4.6 4.7 +void asc_context_quit(void) { 4.8 + asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); 4.9 +} 4.10 + 4.11 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { 4.12 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { 4.13 if (asc_context.windows[i].id == id) { 4.14 @@ -99,6 +103,10 @@ 4.15 } 4.16 4.17 bool asc_loop_next(void) { 4.18 + // reset mouse motion 4.19 + asc_context.input.mouse_xrel = 0; 4.20 + asc_context.input.mouse_yrel = 0; 4.21 + 4.22 // dispatch SDL events 4.23 SDL_Event event; 4.24 while (SDL_PollEvent(&event)) { 4.25 @@ -115,13 +123,20 @@ 4.26 ); 4.27 break; 4.28 } 4.29 + case SDL_MOUSEMOTION: { 4.30 + // accumulate relative motion 4.31 + asc_context.input.mouse_xrel += event.motion.xrel; 4.32 + asc_context.input.mouse_yrel += event.motion.yrel; 4.33 + // update absolute position 4.34 + asc_context.input.mouse_x = event.motion.x; 4.35 + asc_context.input.mouse_y = event.motion.y; 4.36 + break; 4.37 + } 4.38 case SDL_KEYDOWN: 4.39 - // TODO: remove this code and implement key press map instead 4.40 - if (event.key.keysym.sym == SDLK_ESCAPE) 4.41 - return false; 4.42 + asc_context.input.keys[event.key.keysym.scancode] = true; 4.43 break; 4.44 case SDL_KEYUP: 4.45 - // TODO: implement key press map 4.46 + asc_context.input.keys[event.key.keysym.scancode] = false; 4.47 break; 4.48 } 4.49 }
5.1 --- a/test/snake.c Tue Apr 16 22:06:17 2024 +0200 5.2 +++ b/test/snake.c Tue Apr 16 22:20:17 2024 +0200 5.3 @@ -100,7 +100,12 @@ 5.4 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, 5.5 "Fatal Error", asc_get_error(), window->window); 5.6 asc_clear_error(); 5.7 - break; 5.8 + asc_context_quit(); 5.9 + } 5.10 + 5.11 + // quit application on ESC key press 5.12 + if (asc_key_pressed(ESCAPE)) { 5.13 + asc_context_quit(); 5.14 } 5.15 } while (asc_loop_next()); 5.16