Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
BasedOnStyle: Mozilla
IndentWidth: 2
LineEnding: LF
TabWidth: 2
UseTab: Always
ColumnLimit: 120
Language: Cpp
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakAfterDefinitionReturnType: None
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BreakBeforeBraces: Custom
BraceWrapping:
AfterControlStatement: Never
AfterExternBlock: false
AfterFunction: false
AfterStruct: false
AfterUnion: false
SplitEmptyFunction: false
BreakArrays: false
BreakBeforeBinaryOperators: NonAssignment
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<[^/]*\.h'
Priority: -4
CaseSensitive: false
- Regex: '^<.*'
Priority: -3
CaseSensitive: false
- Regex: '^"[^/]*\.h"$'
Priority: -1
CaseSensitive: false
- Regex: '^".*\.h"$'
Priority: -2
CaseSensitive: false
IncludeIsMainRegex: ''
IndentPPDirectives: AfterHash
InsertNewlineAtEOF: true
KeepEmptyLinesAtTheStartOfBlocks: false
PointerAlignment: Left
RemoveBracesLLVM: true
SeparateDefinitionBlocks: Always
SpacesInContainerLiterals: false
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ endif()

message(STATUS "Building project in '${CMAKE_BUILD_TYPE}' mode")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
endif()

set(DESTINATION_EXECUTABLE_FOLDER $<1:${PROJECT_SOURCE_DIR}>/output/bin/$<1:${CMAKE_BUILD_TYPE}>)
set(DESTINATION_LIB_FOLDER $<1:${PROJECT_SOURCE_DIR}>/output/libs/$<1:${CMAKE_BUILD_TYPE}>)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${DESTINATION_EXECUTABLE_FOLDER})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${DESTINATION_LIB_FOLDER})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${DESTINATION_LIB_FOLDER})

set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_C_FLAGS "-Wall -Wextra")

add_subdirectory(src)
12 changes: 8 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
add_executable(gooe main.c)
add_executable(gooe
main.c
)

add_subdirectory(core)

set_source_files_properties(${SOURCES} PROPERTIES LANGUAGE C)
set_target_properties(gooe PROPERTIES LINKER_LANGUAGE C)
Expand All @@ -9,14 +13,14 @@ if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(GOOE_LOG_NONE)
endif()

configure_file(meta.h.in meta.h
configure_file(meta.h.in ${CMAKE_CURRENT_SOURCE_DIR}/meta.h
NEWLINE_STYLE UNIX
)

target_include_directories(gooe
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ./libs
PUBLIC ${GOOE_VENDORS_BUILDS}
PUBLIC ${PROJECT_BINARY_DIR}/src
)

target_link_libraries(gooe gooeLog)
Expand Down Expand Up @@ -46,4 +50,4 @@ add_custom_command (TARGET gooe POST_BUILD
add_custom_command (TARGET gooe POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different
${PROJECT_SOURCE_DIR}/assets $<TARGET_FILE_DIR:gooe>/assets
)
)
1 change: 1 addition & 0 deletions src/const.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#define GOOE_NAME "GooE"
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 720
8 changes: 8 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
target_sources(gooe
PRIVATE gooe.c
PRIVATE window.c
PRIVATE renderer.c
PRIVATE audio.c
PRIVATE assetLoader.c
PRIVATE gameLoop.c
)
80 changes: 80 additions & 0 deletions src/core/assetLoader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <SDL_render.h>
#include <SDL_surface.h>
#include <stdlib.h>

#include <SDL3_image/SDL_image.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <gooeLog/log.h>

#include "core/gooe.h"
#include "core/types.h"

#include "assetLoader.h"

static void* texture = NULL;
static void* music = NULL;
static int imgInit = -1;

void gooe_assetInit() {
imgInit = IMG_Init(IMG_INIT_PNG);
if (imgInit < 0) {
LOG_ERR("GooE assets loader => SDL image could not initialise: %s", SDL_GetError());
exit(1);
}

LOG_INFO("GooE images asset loader initialisation success.");
}

void gooe_assetDestroy() {
if (texture) {
SDL_DestroyTexture(texture);
texture = NULL;
LOG_INFO("GooE textures destroyed.");
}

if (imgInit >= 0) {
IMG_Quit();
LOG_INFO("GooE images asset loader destroyed.");
}

if (music) {
Mix_FreeMusic(music);
music = NULL;
LOG_INFO("GooE music destroyed.");
}
}

void* gooe_assetLoadImage(void* renderer, const char* assetPath) {
SDL_Surface* image = IMG_Load(assetPath);
if (!image) {
LOG_ERR("GooE image asset loader fails => SDL image could load surface: %s", SDL_GetError());
gooe_destroy();
exit(1);
}
LOG_INFO("GooE image successfully loaded as surface");

texture = SDL_CreateTextureFromSurface(renderer, image);
if (!texture) {
LOG_ERR("GooE image asset loader fails => SDL could not create the texture "
"out from the surface: %s",
SDL_GetError());
SDL_DestroySurface(image);
gooe_destroy();
exit(1);
}
LOG_INFO("GooE image asset loader texture created successfully");
SDL_DestroySurface(image);
return texture;
}

void* gooe_assetLoadMusic(const char* assetPath) {
music = Mix_LoadMUS_RW(SDL_RWFromFile(assetPath, "rb"), SDL_TRUE);

if (!music) {
LOG_ERR("SDL mixer could not load the music: %s", SDL_GetError());
gooe_destroy();
exit(1);
}
LOG_INFO("GooE music loaded successfully.");
return music;
}
9 changes: 9 additions & 0 deletions src/core/assetLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "types.h"

void gooe_assetInit();
void gooe_assetDestroy();

void* gooe_assetLoadImage(void* renderer, const char* assetPath);
void* gooe_assetLoadMusic(const char* assetPath);
56 changes: 56 additions & 0 deletions src/core/audio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdlib.h>

#include <SDL3/SDL.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <gooeLog/log.h>

#include "audio.h"
#include "gooe.h"

void _readMusicInfo(void* music);

void gooe_audioInit() {
SDL_AudioSpec spec = {
.freq = MIX_DEFAULT_FREQUENCY,
.format = MIX_DEFAULT_FORMAT,
.channels = MIX_DEFAULT_CHANNELS,
};

if (Mix_OpenAudio(0, &spec) < 0) {
LOG_ERR("GooE audio init fails => SDL mixer could not initialise: %s", SDL_GetError());
gooe_destroy();
exit(1);
}

Mix_VolumeMusic(MIX_MAX_VOLUME);

LOG_INFO("GooE audio initialisation success.");
}

void gooe_audioDestroy() {
Mix_Quit();
LOG_INFO("GooE audio destroyed.");
}

void gooe_audioPlayLoop(void* musicAsset) {
#ifndef NDEBUG
_readMusicInfo(musicAsset);
#endif

Mix_FadeInMusic(musicAsset, -1, 0);
}

void _readMusicInfo(void* asset) {
double loop_start = Mix_GetMusicLoopStartTime(asset);
double loop_end = Mix_GetMusicLoopEndTime(asset);
double loop_length = Mix_GetMusicLoopLengthTime(asset);

#ifdef DEBUG
double duration = Mix_MusicDuration(asset);
LOG_INFO("Loaded music duration is: %fsec", duration);
#endif

if (loop_start > 0.0 && loop_end > 0.0 && loop_length > 0.0)
LOG_INFO("Loop points: start %g sec, end %g sec, length %g sec", loop_start, loop_end, loop_length);
else LOG_INFO("The loaded music is not looping.");
}
8 changes: 8 additions & 0 deletions src/core/audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include "gooe.h"

void gooe_audioInit();
void gooe_audioDestroy();

void gooe_audioPlayLoop(void* musicAsset);
59 changes: 59 additions & 0 deletions src/core/gameLoop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdbool.h>

#include <SDL3/SDL.h>
#include <gooeLog/log.h>

#include "assetLoader.h"
#include "audio.h"
#include "gameLoop.h"
#include "renderer.h"

void game_update(GooE* gooe);
void game_draw(GooE* gooe);

static void* music;
static void* texture;
static double lastFrameTime;

static SDL_FRect srcRect = { .x = 0, .y = 0, .w = 32, .h = 32 };
static SDL_FRect dstRect = { .x = 0.f, .y = 300.f, .w = 32.f, .h = 32.f };

#define TARGET_FRAME_RATE 60
#define FRAME_DURATION_MS 16

void gooe_loop(GooE* gooe) {
gooe_rendererSetClearColor(80, 10, 160);

music = gooe_assetLoadMusic("assets/music/test.ogg");
texture = gooe_assetLoadImage(gooe->renderer, "assets/img/test.png");
gooe_audioPlayLoop(music);

SDL_Event event;
bool shouldQuit = false;
while (!shouldQuit) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT: {
shouldQuit = 1;
}
}
}

// Get delta_time factor converted to seconds to be used to update objects
Uint64 tickCount = SDL_GetTicks();
double delta_time = (tickCount - lastFrameTime) / 1000.0;
lastFrameTime = tickCount;

gooe->deltaTime = delta_time;
game_update(gooe);
game_draw(gooe);
}
}

void game_update(GooE* gooe) { dstRect.x += 20 * gooe->deltaTime; }

void game_draw(GooE* gooe) {
gooe_rendererClearScreen(gooe->renderer);
SDL_RenderTexture(gooe->renderer, texture, &srcRect, &dstRect);
SDL_RenderPresent(gooe->renderer);
}
5 changes: 5 additions & 0 deletions src/core/gameLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "types.h"

void gooe_loop(GooE* gooe);
51 changes: 51 additions & 0 deletions src/core/gooe.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdlib.h>

#include <SDL3/SDL.h>
#include <gooeLog/log.h>

#include "assetLoader.h"
#include "audio.h"
#include "gameLoop.h"
#include "gooe.h"
#include "renderer.h"
#include "types.h"
#include "window.h"

#define SDL_INIT_FLAGS (SDL_INIT_VIDEO | SDL_INIT_AUDIO)

static GooE gooe = {};

void _init();
void _destroy();

GooE* gooe_init() {
_init();
gooe_windowInit(&gooe);
gooe_rendererInit(&gooe);
gooe_audioInit();
gooe_assetInit();
return &gooe;
}

void gooe_run() { gooe_loop(&gooe); }

void gooe_destroy() {
gooe_assetDestroy();
gooe_audioDestroy();
gooe_rendererDestroy(&gooe);
gooe_windowDestroy(&gooe);
_destroy();
}

void _init() {
if (SDL_Init(SDL_INIT_FLAGS) < 0) {
LOG_ERR("GooE init fails => SDL could not initialise: %s", SDL_GetError());
exit(1);
}
LOG_INFO("GooE init success.");
}

void _destroy() {
SDL_Quit();
LOG_INFO("GooE destroyed.");
}
Loading