aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUneven Prankster <unevenprankster@protonmail.com>2023-09-23 19:28:45 -0300
committerUneven Prankster <unevenprankster@protonmail.com>2023-09-23 19:28:45 -0300
commita89f892640cf12f75c7ce18e6e88c70a8d3965ed (patch)
treee917bda607b86cb7c5bd80df2e5abf549d972163
parent83505b7be49dbf7789deb814bd159d9c37181d05 (diff)
things can always be nicer :D
-rw-r--r--assets/gs_full.glsl8
-rw-r--r--assets/src/main.c4
-rw-r--r--build.sh16
-rw-r--r--raylib/config.h2
-rw-r--r--src/audio_sys.c108
-rw-r--r--src/audio_sys.h9
-rw-r--r--src/config.h28
-rw-r--r--src/main.c10
-rw-r--r--src/script_sys.c10
-rw-r--r--src/shader_sys.c100
-rw-r--r--src/text.c64
-rw-r--r--src/text.h23
-rw-r--r--src/texture_sys.c28
13 files changed, 306 insertions, 104 deletions
diff --git a/assets/gs_full.glsl b/assets/gs_full.glsl
index 4b3e3f3..aa6b662 100644
--- a/assets/gs_full.glsl
+++ b/assets/gs_full.glsl
@@ -1,4 +1,4 @@
-@vertex
+#if defined(VERTEX_SHADER)
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
@@ -16,9 +16,7 @@ void main()
gl_Position = mvp*vec4(vertexPosition, 1.0);
}
-@vs_end
-
-@fragment
+#elif defined(FRAG_SHADER)
in vec2 fragTexCoord;
in vec4 fragColor;
@@ -33,4 +31,4 @@ void main()
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
finalColor = vec4(gray, gray, gray, texelColor.a);
}
-@fs_end \ No newline at end of file
+#endif \ No newline at end of file
diff --git a/assets/src/main.c b/assets/src/main.c
index 2a8ab80..188a7ee 100644
--- a/assets/src/main.c
+++ b/assets/src/main.c
@@ -18,8 +18,8 @@ void update(void)
vec2 current_input = get_dir_input();
if(current_input.x != 0 || current_input.y != 0){
- position.x += current_input.x * 0.5f;
- position.y += current_input.y * 0.5f;
+ position.x += current_input.x;
+ position.y += current_input.y;
}
set_active_shader(grayscale);
diff --git a/build.sh b/build.sh
index 3ceb991..e450615 100644
--- a/build.sh
+++ b/build.sh
@@ -24,7 +24,7 @@ set -e
while getopts ":hdusrcq" opt; do
case $opt in
h)
- echo "Usage: ./build-linux.sh [-hdusrcqq]"
+ echo "Usage: ./build.sh [-hdurcqq]"
echo " -h Show this information"
echo " -d Faster builds that have debug symbols, and enable warnings"
echo " -u Run upx* on the executable after compilation (before -r)"
@@ -32,14 +32,10 @@ while getopts ":hdusrcq" opt; do
echo " -c Remove the temp/(debug|release) directory, ie. full recompile"
echo " -q Suppress this script's informational prints"
echo ""
- echo "* This is mostly here to make building simple \"shipping\" versions"
- echo " easier, and it's a very small bit in the build scripts. The option"
- echo " requires that you have upx installed and on your path, of course."
- echo ""
echo "Examples:"
- echo " Build a release build: ./build-linux.sh"
- echo " Build a release build, full recompile: ./build-linux.sh -c"
- echo " Build a debug build and run: ./build-linux.sh -d -r"
+ echo " Build a release build: ./build.sh"
+ echo " Build a release build, full recompile: ./build.sh -c"
+ echo " Build a debug build and run: ./build.sh -d -r"
exit 0
;;
d)
@@ -76,7 +72,7 @@ RAYLIB_SRC="$ROOT_DIR/$RAYLIB_SRC"
TINYCC_SRC="$ROOT_DIR/tinycc"
# Flags
-COMPILATION_FLAGS="-std=gnu99 -Os -flto -s -D__USE_MINGW_ANSI_STDIO=0 -mno-stack-arg-probe -Xlinker --stack=0x200000,0x200000
+COMPILATION_FLAGS="-std=gnu99 -Os -flto=auto -s -D__USE_MINGW_ANSI_STDIO=0 -mno-stack-arg-probe -Xlinker --stack=0x200000,0x200000
-fno-asynchronous-unwind-tables -fwhole-program -Wl,--gc-sections -mavx"
FINAL_FLAGS=$COMPILATION_FLAGS
WARNING_FLAGS="-Wall -Wextra"
@@ -135,7 +131,7 @@ rm *.o
if [ -n "$UPX_IT" ]; then
[ -z "$QUIET" ] && echo "COMPILE-INFO: Packing $GAME_NAME with upx."
- upx $GAME_NAME > /dev/null 2>&1
+ upx $GAME_NAME --brute > /dev/null 2>&1
fi
if [ -n "$RUN_AFTER_BUILD" ]; then
diff --git a/raylib/config.h b/raylib/config.h
index 2593b58..4cbb181 100644
--- a/raylib/config.h
+++ b/raylib/config.h
@@ -35,7 +35,7 @@
#define SUPPORT_MODULE_RSHAPES 1
#define SUPPORT_MODULE_RTEXTURES 1
#define SUPPORT_MODULE_RTEXT 1 // WARNING: It requires SUPPORT_MODULE_RTEXTURES to load sprite font textures
-//#define SUPPORT_MODULE_RMODELS 1
+#define SUPPORT_MODULE_RMODELS 1
#define SUPPORT_MODULE_RAUDIO 1
//------------------------------------------------------------------------------------
diff --git a/src/audio_sys.c b/src/audio_sys.c
new file mode 100644
index 0000000..f164947
--- /dev/null
+++ b/src/audio_sys.c
@@ -0,0 +1,108 @@
+#include "config.h"
+#include <raylib.h>
+
+// Defines
+
+#define MAX_SOUND 8
+
+Sound sounds[MAX_SOUND] = {0};
+Music track = {0};
+
+// Audio things
+
+i32 load_audio(const char* path)
+{
+ i32 idx = -1;
+
+ for(int i = 0; i < MAX_SOUND; ++i){
+ if(!IsSoundReady(sounds[i])){
+ idx = i;
+ sounds[i] = LoadSound(path);
+ break;
+ }
+ }
+
+ assert(idx != -1);
+ return idx;
+}
+
+void play_audio(i32 idx)
+{
+ assert(idx >= 0 && idx < MAX_SOUND);
+ PlaySound(sounds[idx]);
+}
+
+void pause_audio(i32 idx)
+{
+ assert(idx >= 0 && idx < MAX_SOUND);
+ PauseSound(sounds[idx]);
+}
+
+void resume_audio(i32 idx)
+{
+ assert(idx >= 0 && idx < MAX_SOUND);
+ ResumeSound(sounds[idx]);
+}
+
+// Music things //
+
+void load_music(const char* path)
+{
+ track = LoadMusicStream(path);
+}
+
+void play_music(void)
+{
+ PlayMusicStream(track);
+}
+
+void set_music_loop(b32 loop)
+{
+ track.looping = loop;
+}
+
+void set_music_loop_point(f32 point)
+{
+ track.loopPoint = point;
+}
+
+void pause_music(void)
+{
+ PauseMusicStream(track);
+}
+
+void resume_music(void)
+{
+ ResumeMusicStream(track);
+}
+
+// Main funcs
+
+void set_main_vol(f32 vol)
+{
+ assert(vol >= 0.f);
+ SetMasterVolume(vol);
+}
+
+// Update stuff
+
+void update_audio(void)
+{
+ UpdateMusicStream(track);
+}
+
+void unload_active_audio(void)
+{
+ for(int i = 0; i < MAX_SOUND; ++i){
+ if(IsSoundReady(sounds[i])){
+ StopSound(sounds[i]); //It is probably a good idea to do this...
+ UnloadSound(sounds[i]);
+ }
+ }
+}
+
+void unload_current_music(void)
+{
+ StopMusicStream(track);
+ UnloadMusicStream(track);
+} \ No newline at end of file
diff --git a/src/audio_sys.h b/src/audio_sys.h
index 8a5b6cc..9607189 100644
--- a/src/audio_sys.h
+++ b/src/audio_sys.h
@@ -6,11 +6,12 @@
i32 load_audio(const char* path);
void play_audio(i32 idx);
void pause_audio(i32 idx);
+void resume_audio(i32 idx);
// Only one at any given time.
// In the future you can load a second one
// It'll be a copy of the one from my Godot games
-i32 load_music(const char* path);
+void load_music(const char* path);
void play_music(void);
// It would be more convenient if the files had this info
@@ -19,8 +20,10 @@ void set_music_loop(b32 loop);
void set_music_loop_point(f32 point);
void pause_music(void);
-void replay_music(void);
+void resume_music(void);
-void update_audio_sys(void);
+void set_main_vol(f32 vol);
+
+void update_audio(void);
void unload_active_audio(void);
void unload_current_music(void); \ No newline at end of file
diff --git a/src/config.h b/src/config.h
index 40a095c..81a2a51 100644
--- a/src/config.h
+++ b/src/config.h
@@ -1,20 +1,26 @@
#pragma once
-typedef char i8;
-typedef unsigned char u8;
+#include <stddef.h>
+#include <stdint.h>
-typedef short i16;
-typedef unsigned short u16;
+typedef int8_t i8;
+typedef uint8_t u8;
-typedef int i32;
-typedef long long i64;
+typedef int16_t i16;
+typedef uint16_t u16;
-typedef unsigned int u32;
-typedef unsigned long long u64;
+typedef int32_t i32;
+typedef int64_t i64;
+
+typedef uint32_t u32;
+typedef uint64_t u64;
typedef float f32;
typedef double f64;
+typedef unsigned char byte;
+typedef ptrdiff_t size;
+
/*
You may be wondering "why fixed point values???", and that's simple
Floating Point was a mistake and has caused great suffering to countless.
@@ -56,4 +62,8 @@ typedef i8 b32;
# endif
#else
# define assert(c)
-#endif \ No newline at end of file
+#endif
+
+#define sizeof(x) (size)sizeof(x)
+#define countof(a) (sizeof(a) / sizeof(*(a)))
+#define lengthof(s) (countof(s) - 1)
diff --git a/src/main.c b/src/main.c
index 4fc9bc2..4160b94 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,6 +5,7 @@
#include "controller.h"
#include "rand_sys.h"
+#include "audio_sys.h"
int main()
{
@@ -12,6 +13,8 @@ int main()
SetTargetFPS(60);
+ InitAudioDevice();
+
rand_seed();
init_tex_sys();
@@ -22,6 +25,8 @@ int main()
float dt = GetFrameTime();
poll_rumble(dt);
+ update_audio();
+
update_textures();
update_shaders();
@@ -31,10 +36,15 @@ int main()
EndDrawing();
}
+ unload_active_audio();
+ unload_current_music();
+
deinit_script_sys();
unload_active_textures();
unload_active_shaders();
+ CloseAudioDevice();
+
CloseWindow();
return 0;
} \ No newline at end of file
diff --git a/src/script_sys.c b/src/script_sys.c
index 761d874..4b12e64 100644
--- a/src/script_sys.c
+++ b/src/script_sys.c
@@ -24,6 +24,7 @@ static void restart(void)
{
TCCState* new_state = tcc_new();
tcc_set_output_type(new_state, TCC_OUTPUT_MEMORY);
+ tcc_set_options(new_state, "-nostdinc");
tcc_add_symbol(new_state, "load_new_tex", load_new_tex);
tcc_add_symbol(new_state, "load_new_shader", load_new_shader);
@@ -31,6 +32,7 @@ static void restart(void)
tcc_add_symbol(new_state, "draw_text", DrawText);
tcc_add_symbol(new_state, "set_active_shader", set_active_shader);
tcc_add_symbol(new_state, "draw_texture", draw_texture);
+ tcc_add_symbol(new_state, "unload_tex", unload_tex);
tcc_add_symbol(new_state, "reset_active_shader", reset_active_shader);
tcc_add_symbol(new_state, "set_rumble", set_rumble);
@@ -46,13 +48,12 @@ static void restart(void)
tcc_add_symbol(new_state, "get_dir_input", get_dir_input);
if(tcc_add_file(new_state, "assets/src/main.c") == -1){
- TRACELOG(LOG_ERROR, "Compilation failed!\n");
+ tcc_delete(new_state);
return;
}
if(state != NULL)
deinit_script_sys();
-
state = new_state;
tcc_relocate(state, TCC_RELOCATE_AUTO);
@@ -77,13 +78,12 @@ void update_script(void)
restart();
}
- script_update();
+ if(script_update != NULL)
+ script_update();
}
void deinit_script_sys(void)
{
script_exit();
tcc_delete(state);
- unload_active_textures();
- unload_active_shaders();
} \ No newline at end of file
diff --git a/src/shader_sys.c b/src/shader_sys.c
index d19e64b..4d75b6a 100644
--- a/src/shader_sys.c
+++ b/src/shader_sys.c
@@ -1,10 +1,17 @@
#include "utils.h"
#include "config.h"
+#include "text.h"
+
#include <stdlib.h>
+#include <stdio.h>
#define MAX_SHADER_SIZE 8
+static const s8 version_st = String("#version 330\n");
+static const s8 type_markers[2] = {String("#define VERTEX_SHADER 1\n"), String("#define FRAG_SHADER 1\n")};
+static const s8 line_st = String("#line 1\n");
+
// Internal Functions //
// TODO: Compute?
@@ -17,69 +24,41 @@ typedef enum{
// This returns *allocated* memory,
// should be manually free()'ed or some kind of arena used.
-static char* shader_block_preprocess(const char* shader_source, ShaderStep shader_step) {
- static const char* start_markers[2] = {"@vertex", "@fragment"};
- static const char* end_markers[2] = {"@vs_end", "@fs_end"};
- static const char* version_statement = "#version 330\n";
-
- char* parsed_total = NULL;
-
- i32 start_idx = TextFindIndex(shader_source, start_markers[shader_step]);
- if (start_idx == BLANK_DEFAULT){
- TRACELOG(LOG_ERROR, "%s could not be found.", start_markers[shader_step]);
- goto parse_err;
- }
- start_idx += TextLength(start_markers[shader_step]);
-
- i32 end_idx = TextFindIndex(shader_source, end_markers[shader_step]);
- if (end_idx == BLANK_DEFAULT){
- TRACELOG(LOG_ERROR, "%s could not be found. Don't forget to end blocks!", end_markers[shader_step]);
- goto parse_err;
- }
-
- i32 block_sz = end_idx - start_idx;
-
- const char* block = TextSubtext(shader_source, start_idx, block_sz);
-
- i32 total_block_size = block_sz + TextLength(version_statement);
+static s8 shader_block_preprocess(const s8 source, const ShaderStep shader_step)
+{
+ size total = version_st.len + type_markers[shader_step].len + line_st.len + source.len;
- parsed_total = (char*)RL_MALLOC(total_block_size + NULL_TERM_SZ);
+ s8 parsed_total = create_s8(total + NULL_TERM_SZ);
- i32 cursor = 0;
- TextAppend(parsed_total, version_statement, &cursor);
- TextAppend(parsed_total, block, &cursor);
+ size cursor = 0;
+ add_s8(parsed_total, version_st, &cursor);
+ add_s8(parsed_total, type_markers[shader_step], &cursor);
+ add_s8(parsed_total, line_st, &cursor);
+ add_s8(parsed_total, source, &cursor);
+ parsed_total.buf[cursor] = NULL_TERM;
-parse_err:
return parsed_total;
}
-static Shader load_joined_shader(const char* path)
+static Shader load_joined_shader(const s8 path)
{
Shader something = (Shader){0};
- char* shr = LoadFileText(path);
-
- char* vs = shader_block_preprocess(shr, VERTEX);
- if(vs == NULL){
- TRACELOG(LOG_ERROR, "Unsuccessful vertex shader parse for %s", path);
- goto vertex_err;
- }
+ s8 file = load_file_text(path);
+ if(file.len == 0)
+ goto oops;
- char* fs = shader_block_preprocess(shr, FRAGMENT);
- if(fs == NULL){
- TRACELOG(LOG_ERROR, "Unsuccessful fragment shader parse for %s", path);
- goto frag_err;
- }
+ s8 vs = shader_block_preprocess(file, VERTEX);
+ s8 fs = shader_block_preprocess(file, FRAGMENT);
- something = LoadShaderFromMemory(vs, fs);
+ something = LoadShaderFromMemory((const char*)vs.buf, (const char*)fs.buf);
- RL_FREE(fs);
-frag_err:
- RL_FREE(vs);
+ free_s8(&fs);
+ free_s8(&vs);
-vertex_err:
- UnloadFileText(shr);
+ free_s8(&file);
+oops:
return something;
}
@@ -87,29 +66,35 @@ vertex_err:
static Shader shader_slots[MAX_SHADER_SIZE] = {0};
static i32 shader_modtimes[MAX_SHADER_SIZE] = {0};
-static const char* shader_paths[MAX_SHADER_SIZE] = {0};
+static s8 shader_paths[MAX_SHADER_SIZE] = {0};
void init_shader_sys(void)
{
for(i32 i = 0; i < MAX_SHADER_SIZE; ++i){
- shader_slots[i] = (Shader){0};
shader_modtimes[i] = BLANK_DEFAULT;
- shader_paths[i] = NULL;
}
}
i32 load_new_shader(const char* path)
{
+ s8 path_str = cstr_to_s8(path);
+
i32 current_idx = BLANK_DEFAULT;
for(i32 i = 0; i < MAX_SHADER_SIZE; ++i){
+ if(equal_s8(path_str, shader_paths[current_idx])){
+ free_s8(&path_str);
+ return i;
+ }
+
if(shader_slots[i].id <= 0){
current_idx = i;
- shader_slots[current_idx] = load_joined_shader(path);
- shader_modtimes[current_idx] = GetFileModTime(path);
- shader_paths[current_idx] = path;
+ shader_slots[current_idx] = load_joined_shader(path_str);
+ shader_modtimes[current_idx] = GetFileModTime((const char*)path_str.buf);
+ shader_paths[current_idx] = path_str;
break;
}
}
+
//TODO: Handle slots a bit better.
assert(current_idx != BLANK_DEFAULT);
@@ -126,7 +111,7 @@ void update_shaders(void)
{
for(i32 i = 0; i < MAX_SHADER_SIZE; ++i){
if(shader_slots[i].id > 0){
- i32 current_mod = GetFileModTime(shader_paths[i]);
+ i32 current_mod = GetFileModTime((const char*)shader_paths[i].buf);
if(current_mod != shader_modtimes[i]){
shader_modtimes[i] = current_mod;
@@ -148,9 +133,8 @@ void unload_shader(i32 idx)
assert(idx >= 0 && idx < MAX_SHADER_SIZE);
UnloadShader(shader_slots[idx]);
- shader_slots[idx] = (Shader){0};
shader_modtimes[idx] = BLANK_DEFAULT;
- shader_paths[idx] = NULL;
+ free_s8(&shader_paths[idx]);
}
void unload_active_shaders(void)
diff --git a/src/text.c b/src/text.c
new file mode 100644
index 0000000..26c2eab
--- /dev/null
+++ b/src/text.c
@@ -0,0 +1,64 @@
+#include "text.h"
+#include "utils.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+size txt_length(const char *text)
+{
+ size length = 0;
+
+ if(text != NULL){
+ while (*text++) length++;
+ }
+
+ return length;
+}
+
+s8 create_s8(const size sz)
+{
+ return (s8){(u8*)RL_MALLOC(sz), sz};
+}
+
+void free_s8(s8* str)
+{
+ RL_FREE(str->buf);
+}
+
+s8 cstr_to_s8(const char* str)
+{
+ size sz = txt_length(str) + 1;
+ s8 final = (s8){(u8*)RL_MALLOC(sz), sz - 1};
+ memcpy(final.buf, str, sz);
+ return final;
+}
+
+b32 equal_s8(const s8 one, const s8 other)
+{
+ // Get OUTTA here
+ if(one.len != other.len)
+ return false;
+
+ for(i32 i = 0; i < one.len; ++i){
+ if(one.buf[i] != other.buf[i])
+ return false;
+ }
+
+ return true;
+}
+
+void add_s8(s8 dst, const s8 src, size* pos)
+{
+ memcpy(dst.buf + *pos, src.buf, src.len);
+ *pos += src.len;
+}
+
+s8 load_file_text(const s8 path)
+{
+ char* shr = LoadFileText((const char*)path.buf);
+ s8 file = cstr_to_s8(shr);
+ UnloadFileText(shr);
+
+ return file;
+} \ No newline at end of file
diff --git a/src/text.h b/src/text.h
new file mode 100644
index 0000000..9ac01d3
--- /dev/null
+++ b/src/text.h
@@ -0,0 +1,23 @@
+#pragma once
+
+#include "config.h"
+
+#define String(s) (s8){(u8*)s, lengthof(s)}
+typedef struct {
+ u8 *buf;
+ size len;
+} s8;
+
+size txt_length(const char *text);
+
+s8 create_s8(const size sz);
+
+void free_s8(s8* str);
+
+s8 cstr_to_s8(const char* str);
+
+b32 equal_s8(const s8 one, const s8 other);
+
+void add_s8(s8 dst, const s8 src, size* pos);
+
+s8 load_file_text(const s8 path); \ No newline at end of file
diff --git a/src/texture_sys.c b/src/texture_sys.c
index cb7f631..b4c3c4d 100644
--- a/src/texture_sys.c
+++ b/src/texture_sys.c
@@ -1,5 +1,5 @@
#include "utils.h"
-#include "config.h"
+#include "text.h"
#include <stddef.h>
@@ -7,26 +7,32 @@
static Texture2D texture_slots[MAX_TEX_SIZE] = {0};
static i32 texture_modtimes[MAX_TEX_SIZE] = {0};
-static const char* texture_paths[MAX_TEX_SIZE] = {0};
+static s8 texture_paths[MAX_TEX_SIZE] = {0};
void init_tex_sys(void)
{
for(i32 i = 0; i < MAX_TEX_SIZE; ++i){
- texture_slots[i] = (Texture2D){0};
texture_modtimes[i] = BLANK_DEFAULT;
- texture_paths[i] = NULL;
}
}
i32 load_new_tex(const char* path)
{
+ s8 path_str = cstr_to_s8(path);
+
i32 current_idx = BLANK_DEFAULT;
for(i32 i = 0; i < MAX_TEX_SIZE; ++i){
+
+ if(equal_s8(path_str, texture_paths[current_idx])){
+ free_s8(&path_str);
+ return i;
+ }
+
if(texture_slots[i].id <= 0){
current_idx = i;
- texture_slots[current_idx] = LoadTexture(path);
- texture_modtimes[current_idx] = GetFileModTime(path);
- texture_paths[current_idx] = path;
+ texture_slots[current_idx] = LoadTexture((const char*)path_str.buf);
+ texture_modtimes[current_idx] = GetFileModTime((const char*)path_str.buf);
+ texture_paths[current_idx] = path_str;
break;
}
}
@@ -46,13 +52,13 @@ void update_textures(void)
{
for(i32 i = 0; i < MAX_TEX_SIZE; ++i){
if(texture_slots[i].id > 0){
- i32 current_mod = GetFileModTime(texture_paths[i]);
+ i32 current_mod = GetFileModTime((const char*)texture_paths[i].buf);
if(current_mod != texture_modtimes[i]){
texture_modtimes[i] = current_mod;
UnloadTexture(texture_slots[i]);
- texture_slots[i] = LoadTexture(texture_paths[i]);
+ texture_slots[i] = LoadTexture((const char*)texture_paths[i].buf);
}
}
}
@@ -61,10 +67,10 @@ void update_textures(void)
void unload_tex(i32 idx)
{
assert(idx >= 0 && idx < MAX_TEX_SIZE);
+
UnloadTexture(texture_slots[idx]);
- texture_slots[idx] = (Texture2D){0};
texture_modtimes[idx] = BLANK_DEFAULT;
- texture_paths[idx] = NULL;
+ free_s8(&texture_paths[idx]);
}
void unload_active_textures(void)