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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,28 +5,28 @@
#include <stdlib.h>
String load_entire_file(const char* filename) {
auto file = fopen(filename, "rb");
if (!file)
return {};
auto file = fopen(filename, "rb");
if (!file)
return {};
if (fseek(file, 0, SEEK_END))
return {};
if (fseek(file, 0, SEEK_END))
return {};
auto file_length = ftell(file);
if (file_length == -1)
return {};
auto file_length = ftell(file);
if (file_length == -1)
return {};
if (fseek(file, 0, SEEK_SET))
return {};
if (fseek(file, 0, SEEK_SET))
return {};
auto file_mem = malloc(file_length);
if (!file_mem)
return {};
auto file_mem = malloc(file_length);
if (!file_mem)
return {};
if (fread(file_mem, file_length, 1, file) != 1) {
free(file_mem);
return {};
}
if (fread(file_mem, file_length, 1, file) != 1) {
free(file_mem);
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>
void _log(const char* function_name, const char* format, ...) {
printf("[%s] ", function_name);
printf("[%s] ", function_name);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
printf("\n");
}
void _log_error(const char* function_name, const char* format, ...) {
printf("[%s]ERROR: ", function_name);
printf("[%s]ERROR: ", function_name);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
printf("\n");
}

View File

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

View File

@ -28,7 +28,7 @@ static SDL_Window *window;
static SDL_GPUGraphicsPipeline *basic_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 *index_buffer;
@ -152,7 +152,7 @@ static void save_map() {
if (fwrite(&map_width, sizeof(Uint32), 1, file) != 1) {
log_error("fwrite for map_width has failed.");
return;
}
}
if (fwrite(&map_height, sizeof(Uint32), 1, file) != 1) {
log_error("fwrite for map_height has failed.");
@ -213,7 +213,7 @@ static void load_map() {
}
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,
.size = num_bytes,
};
@ -242,7 +242,7 @@ static bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes
SDL_GPUTransferBufferLocation transfer_info = {
.transfer_buffer = transfer_buffer,
.offset = 0,
.offset = 0,
};
SDL_GPUBufferRegion buffer_region = {
@ -414,7 +414,7 @@ static SDL_GPUTexture *create_shader_texture(const char *path) {
if (properties) SDL_DestroyProperties(properties);
Uint32 upload_size = width * height * channels;
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = upload_size,
};
@ -509,7 +509,7 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
if (properties) SDL_DestroyProperties(properties);
Uint32 upload_size = width * height * channels;
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = upload_size,
};
@ -739,25 +739,25 @@ static bool recreate_graphics_pipelines() {
{
.location = 0,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, pos),
},
{
.location = 1,
.buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
.offset = offsetof(Instance, pos),
},
{
.location = 2,
.buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = offsetof(Instance, uv0uv1),
},
{
.location = 3,
.buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
.offset = offsetof(Instance, uv2uv3),
},
};
@ -854,13 +854,13 @@ static bool recreate_graphics_pipelines() {
{
.location = 0,
.buffer_slot = 0,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
.offset = offsetof(Vertex, pos),
},
{
.location = 1,
.buffer_slot = 1,
.format = SDL_GPU_VERTEXELEMENTFORMAT_UINT,
.format = SDL_GPU_VERTEXELEMENTFORMAT_UINT,
.offset = 0,
},
};