convert indentation to spaces

This commit is contained in:
Sven Balzer 2025-03-16 18:56:40 +01:00
parent 09b1f3943a
commit 6a7f54ca5a
9 changed files with 169 additions and 169 deletions

View File

@ -17,7 +17,7 @@ struct VertexShaderOutput {
}; };
struct PixelShaderOutput { struct PixelShaderOutput {
float4 color : SV_TARGET; float4 color : SV_TARGET;
}; };
[shader("vertex")] [shader("vertex")]
@ -45,9 +45,9 @@ Sampler2D<float4> tex1;
[shader("pixel")] [shader("pixel")]
PixelShaderOutput main_fragment(VertexShaderOutput input) { PixelShaderOutput main_fragment(VertexShaderOutput input) {
PixelShaderOutput output; PixelShaderOutput output;
output.color = tex1.Sample(float2(input.uv)); output.color = tex1.Sample(float2(input.uv));
return output; return output;
} }

View File

@ -16,7 +16,7 @@ struct VertexShaderOutput {
}; };
struct PixelShaderOutput { struct PixelShaderOutput {
float4 color : SV_TARGET; float4 color : SV_TARGET;
}; };
[[vk::binding(0, 0)]] [[vk::binding(0, 0)]]
@ -77,9 +77,9 @@ Sampler2D<float4> tex1;
[shader("fragment")] [shader("fragment")]
PixelShaderOutput main_fragment(VertexShaderOutput input) { PixelShaderOutput main_fragment(VertexShaderOutput input) {
PixelShaderOutput output; PixelShaderOutput output;
output.color = tex1.Sample(float2(input.uv)); output.color = tex1.Sample(float2(input.uv));
return output; return output;
} }

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
template <typename F> template <typename F>
struct _defer { struct _defer {
F f; F f;
_defer(F f) : f(f) {}; _defer(F f) : f(f) {};
~_defer() { f(); }; ~_defer() { f(); };
}; };
template <typename F> template <typename F>
_defer<F> MakeDefer(F f) { _defer<F> MakeDefer(F f) {
return _defer<F>(f); return _defer<F>(f);
} }
#define STRING_JOIN(a, b) _STRING_JOIN(a, b) #define STRING_JOIN(a, b) _STRING_JOIN(a, b)

View File

@ -11,90 +11,90 @@
#include "glyphs.h" #include "glyphs.h"
struct Glyph { struct Glyph {
int x_off; int x_off;
int y_off; int y_off;
char* data; char* data;
}; };
Glyph_Coord glyph_coords[512]; Glyph_Coord glyph_coords[512];
int main() { int main() {
stbtt_fontinfo font_info; stbtt_fontinfo font_info;
stbrp_context packer_context; stbrp_context packer_context;
stbrp_node packer_nodes[600]; stbrp_node packer_nodes[600];
stbrp_rect packer_rects[100]; stbrp_rect packer_rects[100];
Glyph glyphs[512]; Glyph glyphs[512];
String font_file = load_entire_file("../assets/fonts/Lexend-Regular.ttf"); String font_file = load_entire_file("../assets/fonts/Lexend-Regular.ttf");
if (!font_file.length) { if (!font_file.length) {
log_error("Loading font file has failed."); log_error("Loading font file has failed.");
return 1; return 1;
} }
if (!stbtt_InitFont(&font_info, (unsigned char*)font_file.data, 0)) { if (!stbtt_InitFont(&font_info, (unsigned char*)font_file.data, 0)) {
log_error("Init Font has failed."); log_error("Init Font has failed.");
return 1; return 1;
} }
float font_scale = stbtt_ScaleForPixelHeight(&font_info, 48); float font_scale = stbtt_ScaleForPixelHeight(&font_info, 48);
stbrp_init_target(&packer_context, 512, 512, packer_nodes, sizeof(packer_nodes) / sizeof(*packer_nodes)); stbrp_init_target(&packer_context, 512, 512, packer_nodes, sizeof(packer_nodes) / sizeof(*packer_nodes));
int rect_number = 0; int rect_number = 0;
for (int i = ' '; i <= '~'; i++) { for (int i = ' '; i <= '~'; i++) {
int glyph_width; int glyph_width;
int glyph_height; int glyph_height;
int glyph_offset_x; int glyph_offset_x;
int glyph_offset_y; int glyph_offset_y;
glyphs[rect_number].data = (char*) stbtt_GetCodepointBitmap(&font_info, font_scale, font_scale, i, &glyph_width, &glyph_height, &glyph_offset_x, &glyph_offset_y); glyphs[rect_number].data = (char*) stbtt_GetCodepointBitmap(&font_info, font_scale, font_scale, i, &glyph_width, &glyph_height, &glyph_offset_x, &glyph_offset_y);
glyphs[rect_number].x_off = glyph_offset_x; glyphs[rect_number].x_off = glyph_offset_x;
glyphs[rect_number].y_off = glyph_offset_y; glyphs[rect_number].y_off = glyph_offset_y;
packer_rects[rect_number].id = i; packer_rects[rect_number].id = i;
packer_rects[rect_number].w = glyph_width; packer_rects[rect_number].w = glyph_width;
packer_rects[rect_number].h = glyph_height; packer_rects[rect_number].h = glyph_height;
rect_number++; rect_number++;
} }
stbrp_pack_rects(&packer_context, packer_rects, rect_number); stbrp_pack_rects(&packer_context, packer_rects, rect_number);
char* glyph_atlas = (char*) malloc(512 * 512); char* glyph_atlas = (char*) malloc(512 * 512);
for (int x = 0; x < 512; x++) { for (int x = 0; x < 512; x++) {
for (int y = 0; y < 512; y++) { for (int y = 0; y < 512; y++) {
glyph_atlas[y * 512 + x] = 0; glyph_atlas[y * 512 + x] = 0;
} }
} }
for (int i = 0; i < rect_number; i++) { for (int i = 0; i < rect_number; i++) {
glyph_coords[i].x = packer_rects[i].x / 512.0f; glyph_coords[i].x = packer_rects[i].x / 512.0f;
glyph_coords[i].y = packer_rects[i].y / 512.0f; glyph_coords[i].y = packer_rects[i].y / 512.0f;
glyph_coords[i].width = packer_rects[i].w / 512.0f; glyph_coords[i].width = packer_rects[i].w / 512.0f;
glyph_coords[i].height = packer_rects[i].h / 512.0f; glyph_coords[i].height = packer_rects[i].h / 512.0f;
for (int x = 0; x < packer_rects[i].w; x++) { for (int x = 0; x < packer_rects[i].w; x++) {
for (int y = 0; y < packer_rects[i].h; y++) { for (int y = 0; y < packer_rects[i].h; y++) {
glyph_atlas[(y + packer_rects[i].y) * 512 + x + packer_rects[i].x] = glyphs[i].data[y * packer_rects[i].w + x]; glyph_atlas[(y + packer_rects[i].y) * 512 + x + packer_rects[i].x] = glyphs[i].data[y * packer_rects[i].w + x];
} }
} }
} }
FILE* file = fopen("../assets/fonts/glyph_coords_lexend.co", "wb"); FILE* file = fopen("../assets/fonts/glyph_coords_lexend.co", "wb");
if (!file) { if (!file) {
log_error("Glyph coords file creation has failed."); log_error("Glyph coords file creation has failed.");
return 1; return 1;
} }
defer(fclose(file)); defer(fclose(file));
if (fwrite(glyph_coords, sizeof(*glyph_coords) * rect_number, 1, file) != 1) { if (fwrite(glyph_coords, sizeof(*glyph_coords) * rect_number, 1, file) != 1) {
log_error("fwrite for glyph_coords has failed."); log_error("fwrite for glyph_coords has failed.");
return 1; return 1;
} }
if (!stbi_write_bmp("../assets/fonts/glyph_atlas_lexend.bmp", 512, 512, 1, glyph_atlas)) { if (!stbi_write_bmp("../assets/fonts/glyph_atlas_lexend.bmp", 512, 512, 1, glyph_atlas)) {
log_error("stbi_write_bmp has failed."); log_error("stbi_write_bmp has failed.");
return 1; return 1;
} }
return 0; return 0;
} }

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
struct Glyph_Coord { struct Glyph_Coord {
float x; float x;
float y; float y;
float width; float width;
float height; float height;
}; };

View File

@ -5,28 +5,28 @@
#include <stdlib.h> #include <stdlib.h>
String load_entire_file(const char* filename) { String load_entire_file(const char* filename) {
auto file = fopen(filename, "rb"); auto file = fopen(filename, "rb");
if (!file) if (!file)
return {}; return {};
if (fseek(file, 0, SEEK_END)) if (fseek(file, 0, SEEK_END))
return {}; return {};
auto file_length = ftell(file); auto file_length = ftell(file);
if (file_length == -1) if (file_length == -1)
return {}; return {};
if (fseek(file, 0, SEEK_SET)) if (fseek(file, 0, SEEK_SET))
return {}; return {};
auto file_mem = malloc(file_length); auto file_mem = malloc(file_length);
if (!file_mem) if (!file_mem)
return {}; return {};
if (fread(file_mem, file_length, 1, file) != 1) { if (fread(file_mem, file_length, 1, file) != 1) {
free(file_mem); free(file_mem);
return {}; return {};
} }
return { (size_t)file_length, (char*)file_mem }; return { (size_t)file_length, (char*)file_mem };
} }

View File

@ -3,23 +3,23 @@
#include <stdarg.h> #include <stdarg.h>
void _log(const char* function_name, const char* format, ...) { void _log(const char* function_name, const char* format, ...) {
printf("[%s] ", function_name); printf("[%s] ", function_name);
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vprintf(format, args); vprintf(format, args);
va_end(args); va_end(args);
printf("\n"); printf("\n");
} }
void _log_error(const char* function_name, const char* format, ...) { void _log_error(const char* function_name, const char* format, ...) {
printf("[%s]ERROR: ", function_name); printf("[%s]ERROR: ", function_name);
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vprintf(format, args); vprintf(format, args);
va_end(args); va_end(args);
printf("\n"); printf("\n");
} }

View File

@ -4,65 +4,65 @@
struct String { struct String {
size_t length; size_t length;
char* data; char* data;
constexpr String() : length(0), data(0) {}; constexpr String() : length(0), data(0) {};
constexpr String(size_t length, char* data) : length(length), data(data) {}; constexpr String(size_t length, char* data) : length(length), data(data) {};
template<size_t _len> constexpr String(const char(&data)[_len]) : length(_len - 1), data((char*)data) {}; template<size_t _len> constexpr String(const char(&data)[_len]) : length(_len - 1), data((char*)data) {};
char operator[](int index) { char operator[](int index) {
if (index < 0) { if (index < 0) {
return 0; return 0;
} }
if (index < length) { if (index < length) {
return data[index]; return data[index];
} }
return 0; return 0;
} }
}; };
inline String operator""_str(const char* str, size_t length) { inline String operator""_str(const char* str, size_t length) {
return { length, (char*)str }; return { length, (char*)str };
} }
inline bool operator==(String a, String b) { inline bool operator==(String a, String b) {
if (a.length != b.length) if (a.length != b.length)
return false; return false;
if (a.data == b.data) if (a.data == b.data)
return true; return true;
for (int i = 0; i < a.length; i++) for (int i = 0; i < a.length; i++)
if (a[i] != b[i]) if (a[i] != b[i])
return false; return false;
return true; return true;
} }
inline void advance(String& s, int num = 1) { inline void advance(String& s, int num = 1) {
int to_advance = min(s.length, num); int to_advance = min(s.length, num);
s.data = s.data + to_advance; s.data = s.data + to_advance;
s.length = s.length - to_advance; s.length = s.length - to_advance;
} }
inline bool starts_with(String s, String start) { inline bool starts_with(String s, String start) {
if (s.length < start.length) { if (s.length < start.length) {
return false; return false;
} }
for (int i = 0; i < start.length; i++) { for (int i = 0; i < start.length; i++) {
if (s[i] != start[i]) { if (s[i] != start[i]) {
return false; return false;
} }
} }
return true; return true;
} }
template <typename T> template <typename T>
inline T read(String& file) { inline T read(String& file) {
if (file.length < sizeof(T)) if (file.length < sizeof(T))
return {}; return {};
T value = *(T*)file.data; T value = *(T*)file.data;
advance(file, sizeof(T)); advance(file, sizeof(T));
return value; return value;
} }

View File

@ -28,7 +28,7 @@ static SDL_Window *window;
static SDL_GPUGraphicsPipeline *basic_graphics_pipeline; static SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
static SDL_GPUGraphicsPipeline *world_graphics_pipeline; static SDL_GPUGraphicsPipeline *world_graphics_pipeline;
static SDL_GPUSampler *point_sampler; static SDL_GPUSampler *point_sampler;
static SDL_GPUBuffer *vertex_buffer; static SDL_GPUBuffer *vertex_buffer;
static SDL_GPUBuffer *index_buffer; static SDL_GPUBuffer *index_buffer;
@ -213,7 +213,7 @@ static void load_map() {
} }
static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) { static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = { SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = num_bytes, .size = num_bytes,
}; };
@ -242,7 +242,7 @@ static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes
SDL_GPUTransferBufferLocation transfer_info = { SDL_GPUTransferBufferLocation transfer_info = {
.transfer_buffer = transfer_buffer, .transfer_buffer = transfer_buffer,
.offset = 0, .offset = 0,
}; };
SDL_GPUBufferRegion buffer_region = { SDL_GPUBufferRegion buffer_region = {
@ -414,7 +414,7 @@ static SDL_GPUTexture *create_shader_texture(const char *path) {
if (properties) SDL_DestroyProperties(properties); if (properties) SDL_DestroyProperties(properties);
Uint32 upload_size = width * height * channels; Uint32 upload_size = width * height * channels;
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = { SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = upload_size, .size = upload_size,
}; };
@ -509,7 +509,7 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
if (properties) SDL_DestroyProperties(properties); if (properties) SDL_DestroyProperties(properties);
Uint32 upload_size = width * height * channels; Uint32 upload_size = width * height * channels;
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = { SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = upload_size, .size = upload_size,
}; };
@ -739,25 +739,25 @@ static bool recreate_graphics_pipelines() {
{ {
.location = 0, .location = 0,
.buffer_slot = 0, .buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, pos), .offset = offsetof(Vertex, pos),
}, },
{ {
.location = 1, .location = 1,
.buffer_slot = 1, .buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
.offset = offsetof(Instance, pos), .offset = offsetof(Instance, pos),
}, },
{ {
.location = 2, .location = 2,
.buffer_slot = 1, .buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = offsetof(Instance, uv0uv1), .offset = offsetof(Instance, uv0uv1),
}, },
{ {
.location = 3, .location = 3,
.buffer_slot = 1, .buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = offsetof(Instance, uv2uv3), .offset = offsetof(Instance, uv2uv3),
}, },
}; };
@ -854,13 +854,13 @@ static bool recreate_graphics_pipelines() {
{ {
.location = 0, .location = 0,
.buffer_slot = 0, .buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, pos), .offset = offsetof(Vertex, pos),
}, },
{ {
.location = 1, .location = 1,
.buffer_slot = 1, .buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_UINT, .format = SDL_GPU_VERTEXELEMENTFORMAT_UINT,
.offset = 0, .offset = 0,
}, },
}; };