ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

commit f45455ff10f8719153768b80d7a58b0cb1fb0108
parent 8d688b8a74ec8b51db71103c8e4ef7846dcd2816
Author: amin <dev@aminmesbah.com>
Date:   Sat, 11 Nov 2017 03:05:40 +0000

Use C99 style struct initialization

Safer (initializes struct members to 0) and easier to read.

C99 Standard 6.7.8.21:

    If there are fewer initializers in a brace-enclosed list than there
    are elements or members of an aggregate, or fewer characters in a
    string literal used to initialize an array of known size than there
    are elements in the array, the remainder of the aggregate shall be
    initialized implicitly the same as objects that have static storage
    duration.

FossilOrigin-Name: cf73004f75d8aae9e2df28f9dbd96abed6b484f5e244d028c96888ad458a9b3d
Diffstat:
Msrc/entity.c | 8+++++---
Msrc/platform_sdl.c | 29+++++++++++++----------------
2 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/src/entity.c b/src/entity.c @@ -9,9 +9,11 @@ struct Vec2d vec2d_add(float angle1, float length1, float angle2, float length2) float x = sinf(angle1) * length1 + sinf(angle2) * length2; float y = cosf(angle1) * length1 + cosf(angle2) * length2; - struct Vec2d new_vec; - new_vec.angle = 0.5 * M_PI - atan2f(y, x); - new_vec.length = hypotf(x, y); + struct Vec2d new_vec = + { + .angle = 0.5 * M_PI - atan2f(y, x), + .length = hypotf(x, y), + }; return new_vec; } diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -12,12 +12,12 @@ extern bool PAUSED; -static struct SDLOffscreenBuffer global_back_buffer; +static struct SDLOffscreenBuffer global_back_buffer = {0}; struct SDLWindowDimension sdl_get_window_dimension(SDL_Window *window) { - struct SDLWindowDimension result; + struct SDLWindowDimension result = {0}; SDL_GetWindowSize(window, &result.width, &result.height); return(result); } @@ -238,15 +238,10 @@ int main(int argc, char *argv[]) uint64_t lag = 0; uint64_t previous_ms = (SDL_GetPerformanceCounter() * SECOND) / SDL_GetPerformanceFrequency(); - struct GameState game_state; + struct GameState game_state = {0}; game_init(&game_state, dimension.width, dimension.height); - struct GameControllerInput controller_input; - controller_input.controller_index = 0; - controller_input.left_stick_x = 0; - controller_input.left_stick_y = 0; - controller_input.right_stick_x = 0; - controller_input.right_stick_y = 0; + struct GameControllerInput controller_input = {0}; while (running) { @@ -265,13 +260,15 @@ int main(int argc, char *argv[]) dimension = sdl_get_window_dimension(window); - struct OffscreenBuffer buffer; - // WARNING: these pointers are aliased until the end of the - // loop - buffer.memory = global_back_buffer.memory; - buffer.width = global_back_buffer.width; - buffer.height = global_back_buffer.height; - buffer.pitch = global_back_buffer.pitch; + struct OffscreenBuffer buffer = + { + // WARNING: these pointers are aliased until the end of the + // loop + .memory = global_back_buffer.memory, + .width = global_back_buffer.width, + .height = global_back_buffer.height, + .pitch = global_back_buffer.pitch, + }; if (PAUSED) {