major refactor and add a camera
change asset loading of images to be sRGB use wayland by default on linux unless we are running under renderdoc changed shaders to be combined vertex and fragment in a single file require Vulkan 1.3 and enable shaderDrawParameters
This commit is contained in:
+390
-278
@@ -13,24 +13,24 @@
|
||||
#include "math_graphics.h"
|
||||
#include "load_entire_file.h"
|
||||
#include "stb_image.h"
|
||||
#include "../assets/shader/basic_vertex_shader.h"
|
||||
#include "../assets/shader/basic_pixel_shader.h"
|
||||
|
||||
#include "../assets/shader/basic.h"
|
||||
#include "../assets/shader/world.h"
|
||||
|
||||
using namespace M;
|
||||
|
||||
SDL_GPUDevice *device;
|
||||
SDL_Window *window;
|
||||
|
||||
SDL_GPUShader *basic_vertex_shader;
|
||||
SDL_GPUShader *basic_pixel_shader;
|
||||
|
||||
SDL_GPUGraphicsPipeline *basic_graphics_pipeline;
|
||||
SDL_GPUGraphicsPipeline *world_graphics_pipeline;
|
||||
SDL_GPUSampler *point_sampler;
|
||||
|
||||
SDL_GPUBuffer *vertex_buffer;
|
||||
SDL_GPUBuffer *index_buffer;
|
||||
SDL_GPUBuffer *tiles_instance_buffer;
|
||||
SDL_GPUBuffer *player_instance_buffer;
|
||||
SDL_GPUBuffer *world_buffer;
|
||||
SDL_GPUBuffer *tile_infos_buffer;
|
||||
|
||||
Sint32 window_width;
|
||||
Sint32 window_height;
|
||||
@@ -40,16 +40,18 @@ bool Running = true;
|
||||
#define view_width 16
|
||||
#define view_height 9
|
||||
|
||||
M4x4 projection_matrix = projection (radians(45.0f), 1.0f, 0.001f);
|
||||
M4x4 inverse_projection_matrix = inverse_projection(radians(45.0f), 1.0f, 0.001f);
|
||||
|
||||
struct Vertex {
|
||||
V4 pos;
|
||||
V2 uv0uv1;
|
||||
V3 pos;
|
||||
};
|
||||
|
||||
Vertex vertices[] = {
|
||||
{{ -1, 1, 1, 1 }, {0, 0}},
|
||||
{{ 1, 1, 1, 1 }, {1, 0}},
|
||||
{{ 1, -1, 1, 1 }, {1, 1}},
|
||||
{{ -1, -1, 1, 1 }, {0, 1}},
|
||||
{{ -0.5f, 0.5f, 0 }},
|
||||
{{ 0.5f, 0.5f, 0 }},
|
||||
{{ 0.5f, -0.5f, 0 }},
|
||||
{{ -0.5f, -0.5f, 0 }},
|
||||
};
|
||||
|
||||
Uint16 indices[] = {
|
||||
@@ -58,45 +60,43 @@ Uint16 indices[] = {
|
||||
};
|
||||
|
||||
struct Instance {
|
||||
V4 pos_size;
|
||||
Uint32 tile_type;
|
||||
V2 pos;
|
||||
V4 uv0uv1;
|
||||
V4 uv2uv3;
|
||||
};
|
||||
|
||||
Instance tiles_instances[view_width * view_height] = {
|
||||
Instance player_instance = { { 0.0f, 0.0f }, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
||||
|
||||
};
|
||||
|
||||
Instance player_instance = { { 0.5f + 0.5f / view_width, 0.5f, 1.0f / view_width, 1.0f / view_height }, 0, { 0, 0, 1, 0 }, { 1, 1, 0, 1 }};
|
||||
|
||||
Uint32 map_width = view_width;
|
||||
Uint32 map_height = view_height;
|
||||
Sint32 map_width;
|
||||
Sint32 map_height;
|
||||
|
||||
Uint32* map_tiles;
|
||||
|
||||
struct Player {
|
||||
Sint64 pos_x;
|
||||
Sint64 pos_y;
|
||||
Sint32 pos_x;
|
||||
Sint32 pos_y;
|
||||
};
|
||||
|
||||
Player player = {
|
||||
.pos_x = 8,
|
||||
.pos_y = 6,
|
||||
};
|
||||
Player player;
|
||||
|
||||
struct PerFrame {
|
||||
float aspect_ratio;
|
||||
float empty[3];
|
||||
float aspect_ratio;
|
||||
float fovy_degrees;
|
||||
float camera_x;
|
||||
float camera_y;
|
||||
float camera_z;
|
||||
Uint32 map_width;
|
||||
};
|
||||
|
||||
PerFrame per_frame = {1};
|
||||
PerFrame per_frame = { 1.0f, 45.0f, 0, 0, -10 };
|
||||
|
||||
typedef struct {
|
||||
Uint32 type;
|
||||
Uint16 type;
|
||||
const char *asset_path;
|
||||
|
||||
smol_atlas_item_t *atlas_item;
|
||||
V2 uv_min;
|
||||
V2 uv_max;
|
||||
} TileInfo;
|
||||
|
||||
TileInfo tile_infos[] = {
|
||||
@@ -114,6 +114,8 @@ TileInfo tile_infos[] = {
|
||||
int tile_atlas_size = 256;
|
||||
smol_atlas_t *tile_atlas;
|
||||
|
||||
V4 cpu_tile_infos_buffer[SDL_arraysize(tile_infos)];
|
||||
|
||||
Sint32 selected_tile = -1;
|
||||
Sint32 selected_rotation = 0;
|
||||
|
||||
@@ -190,75 +192,6 @@ void load_map() {
|
||||
log("Loading map was successful.");
|
||||
}
|
||||
|
||||
void change_map_size(char direction, int amount) {
|
||||
Uint32* old_map = map_tiles;
|
||||
auto old_map_width = map_width;
|
||||
auto old_map_height = map_height;
|
||||
|
||||
Sint32 new_x_offset = 0;
|
||||
Sint32 new_y_offset = 0;
|
||||
Sint32 old_x_offset = 0;
|
||||
Sint32 old_y_offset = 0;
|
||||
|
||||
Sint32 to_fill_width = map_width;
|
||||
Sint32 to_fill_height = map_height;
|
||||
Sint32 to_fill_x_offset = 0;
|
||||
Sint32 to_fill_y_offset = 0;
|
||||
|
||||
if (direction == 'W') {
|
||||
player.pos_x = player.pos_x + amount;
|
||||
map_width += amount;
|
||||
to_fill_width = amount;
|
||||
|
||||
if (amount < 0)
|
||||
old_x_offset = -amount;
|
||||
else
|
||||
new_x_offset = amount;
|
||||
}
|
||||
|
||||
if (direction == 'N') {
|
||||
player.pos_y = player.pos_y + amount;
|
||||
map_height += amount;
|
||||
to_fill_height = amount;
|
||||
|
||||
if (amount < 0)
|
||||
old_y_offset = -amount;
|
||||
else
|
||||
new_y_offset = amount;
|
||||
}
|
||||
|
||||
if (direction == 'E') {
|
||||
map_width += amount;
|
||||
to_fill_width = amount;
|
||||
to_fill_x_offset = old_map_width;
|
||||
}
|
||||
|
||||
if (direction == 'S') {
|
||||
map_height += amount;
|
||||
to_fill_height = amount;
|
||||
to_fill_y_offset = old_map_height;
|
||||
}
|
||||
|
||||
map_tiles = (Uint32*)malloc(map_width * map_height * sizeof(Uint32));
|
||||
|
||||
for (int y = 0; y < min(old_map_height, map_height); y++) {
|
||||
for (int x = 0; x < min(old_map_width, map_width); x++) {
|
||||
map_tiles[(y + new_y_offset) * map_width + (x + new_x_offset)] = old_map[(y + old_y_offset) * old_map_width + (x + old_x_offset)];
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < to_fill_height; y++) {
|
||||
for (int x = 0; x < to_fill_width; x++) {
|
||||
map_tiles[(y + to_fill_y_offset) * map_width + (x + to_fill_x_offset)] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
player.pos_x = clamp(0, player.pos_x, map_width - 1);
|
||||
player.pos_y = clamp(0, player.pos_y, map_height - 1);
|
||||
|
||||
free(old_map);
|
||||
}
|
||||
|
||||
bool update_buffer(SDL_GPUBuffer *buffer, Uint32 offset, Uint32 num_bytes, void *data) {
|
||||
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
@@ -341,6 +274,83 @@ SDL_GPUBuffer *create_buffer(SDL_GPUBufferUsageFlags usage, Uint32 num_bytes, vo
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void change_map_size(char direction, int amount) {
|
||||
SDL_GPUBuffer *old_world_buffer = world_buffer;
|
||||
Uint32* old_map = map_tiles;
|
||||
auto old_map_width = map_width;
|
||||
auto old_map_height = map_height;
|
||||
|
||||
Sint32 new_x_offset = 0;
|
||||
Sint32 new_y_offset = 0;
|
||||
Sint32 old_x_offset = 0;
|
||||
Sint32 old_y_offset = 0;
|
||||
|
||||
Sint32 to_fill_width = map_width;
|
||||
Sint32 to_fill_height = map_height;
|
||||
Sint32 to_fill_x_offset = 0;
|
||||
Sint32 to_fill_y_offset = 0;
|
||||
|
||||
if (direction == 'W') {
|
||||
player.pos_x = player.pos_x + amount;
|
||||
map_width += amount;
|
||||
to_fill_width = amount;
|
||||
|
||||
if (amount < 0)
|
||||
old_x_offset = -amount;
|
||||
else
|
||||
new_x_offset = amount;
|
||||
}
|
||||
|
||||
if (direction == 'N') {
|
||||
player.pos_y = player.pos_y + amount;
|
||||
map_height += amount;
|
||||
to_fill_height = amount;
|
||||
|
||||
if (amount < 0)
|
||||
old_y_offset = -amount;
|
||||
else
|
||||
new_y_offset = amount;
|
||||
}
|
||||
|
||||
if (direction == 'E') {
|
||||
map_width += amount;
|
||||
to_fill_width = amount;
|
||||
to_fill_x_offset = old_map_width;
|
||||
}
|
||||
|
||||
if (direction == 'S') {
|
||||
map_height += amount;
|
||||
to_fill_height = amount;
|
||||
to_fill_y_offset = old_map_height;
|
||||
}
|
||||
|
||||
map_tiles = (Uint32*)malloc(map_width * map_height * sizeof(Uint32));
|
||||
|
||||
for (int y = 0; y < min(old_map_height, map_height); y++) {
|
||||
for (int x = 0; x < min(old_map_width, map_width); x++) {
|
||||
map_tiles[(y + new_y_offset) * map_width + (x + new_x_offset)] = old_map[(y + old_y_offset) * old_map_width + (x + old_x_offset)];
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < to_fill_height; y++) {
|
||||
for (int x = 0; x < to_fill_width; x++) {
|
||||
map_tiles[(y + to_fill_y_offset) * map_width + (x + to_fill_x_offset)] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
player.pos_x = clamp(0, player.pos_x, map_width - 1);
|
||||
player.pos_y = clamp(0, player.pos_y, map_height - 1);
|
||||
|
||||
world_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, map_width * map_height * 4, map_tiles, "world_buffer");
|
||||
if (!world_buffer) {
|
||||
log_error("Failed to create buffer. Exiting.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
free(old_map);
|
||||
SDL_ReleaseGPUBuffer(device, old_world_buffer);
|
||||
}
|
||||
|
||||
SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
int width = 0, height = 0, channels = 0;
|
||||
stbi_uc *data = stbi_load(path, &width, &height, &channels, 0);
|
||||
@@ -351,7 +361,7 @@ SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
||||
|
||||
if (format == SDL_GPU_TEXTUREFORMAT_INVALID) {
|
||||
log_error("Failed to find texture format for texture (\"%s\").", path);
|
||||
@@ -448,7 +458,7 @@ SDL_GPUTexture *create_shader_texture(const char *path) {
|
||||
SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
||||
if (channels == 4) format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
|
||||
|
||||
if (format == SDL_GPU_TEXTUREFORMAT_INVALID) {
|
||||
log_error("Failed to find texture format for texture.");
|
||||
@@ -537,9 +547,9 @@ SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int wi
|
||||
return texture;
|
||||
}
|
||||
|
||||
void blit(char *dst, Sint32 dst_width, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 width, Sint32 height, int components = 4) {
|
||||
void blit(char *dst, Sint32 dst_pitch, Sint32 dst_x, Sint32 dst_y, char *src, Sint32 src_pitch, Sint32 width, Sint32 height, int components = 4) {
|
||||
for (Sint32 y = 0; y < height; y++)
|
||||
memmove(&dst[((dst_y + y) * dst_width + dst_x) * components], &src[y * width * components], width * components);
|
||||
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
||||
}
|
||||
|
||||
bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamplerBinding *image, const ImVec2& image_size, const ImVec2& uv0 = ImVec2(0, 0), const ImVec2& uv1 = ImVec2(1, 1), Uint8 orientation = 0) {
|
||||
@@ -564,7 +574,30 @@ bool SelectableImage(const char *label, bool selected, SDL_GPUTextureSamplerBind
|
||||
return pressed;
|
||||
}
|
||||
|
||||
ImVec4 linear_to_sRGB(ImVec4 linear) {
|
||||
float red = linear.x <= 0.0031308f ? 12.92f * linear.x : 1.055f * powf(linear.x, 1.0f / 2.4f) - 0.055;
|
||||
float green = linear.y <= 0.0031308f ? 12.92f * linear.y : 1.055f * powf(linear.y, 1.0f / 2.4f) - 0.055;
|
||||
float blue = linear.z <= 0.0031308f ? 12.92f * linear.z : 1.055f * powf(linear.z, 1.0f / 2.4f) - 0.055;
|
||||
|
||||
return ImVec4(red, green, blue, linear.w);
|
||||
}
|
||||
|
||||
ImVec4 sRGB_to_linear(ImVec4 linear) {
|
||||
float red = linear.x <= 0.0031308f ? linear.x / 12.92f : powf((linear.x + 0.055) / 1.055, 2.4f);
|
||||
float green = linear.y <= 0.0031308f ? linear.y / 12.92f : powf((linear.y + 0.055) / 1.055, 2.4f);
|
||||
float blue = linear.z <= 0.0031308f ? linear.z / 12.92f : powf((linear.z + 0.055) / 1.055, 2.4f);
|
||||
|
||||
return ImVec4(red, green, blue, linear.w);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
load_map();
|
||||
|
||||
#ifdef SDL_PLATFORM_LINUX
|
||||
if (!getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"))
|
||||
SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "wayland,x11");
|
||||
#endif
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
|
||||
log_error("Failed to initialize SDL (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
@@ -587,93 +620,73 @@ int main(int argc, char **argv) {
|
||||
log_error("Failed to claim window for gpu device (%s). Exiting.", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_SetGPUSwapchainParameters(device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, SDL_GPU_PRESENTMODE_VSYNC);
|
||||
SDL_SetGPUSwapchainParameters(device, window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR_LINEAR, SDL_GPU_PRESENTMODE_VSYNC);
|
||||
|
||||
SDL_GPUShaderCreateInfo basic_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic_vertex_shader),
|
||||
.code = SPIRV_basic_vertex_shader,
|
||||
.entrypoint = "main",
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
basic_vertex_shader = SDL_CreateGPUShader(device, &basic_vertex_shader_info);
|
||||
SDL_GPUShader *basic_vertex_shader = SDL_CreateGPUShader(device, &basic_vertex_shader_info);
|
||||
if (!basic_vertex_shader) {
|
||||
log_error("Failed to create basic vertex shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo basic_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_basic_pixel_shader),
|
||||
.code = SPIRV_basic_pixel_shader,
|
||||
.entrypoint = "main",
|
||||
.code_size = SDL_arraysize(SPIRV_basic),
|
||||
.code = SPIRV_basic,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
basic_pixel_shader = SDL_CreateGPUShader(device, &basic_pixel_shader_info);
|
||||
SDL_GPUShader *basic_pixel_shader = SDL_CreateGPUShader(device, &basic_pixel_shader_info);
|
||||
if (!basic_pixel_shader) {
|
||||
log_error("Failed to create basic pixel shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||
{
|
||||
.slot = 0,
|
||||
.pitch = sizeof(Vertex),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
|
||||
},
|
||||
{
|
||||
.slot = 1,
|
||||
.pitch = sizeof(Instance),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_INSTANCE,
|
||||
.instance_step_rate = 1,
|
||||
},
|
||||
SDL_GPUShaderCreateInfo world_vertex_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_vertex",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_VERTEX,
|
||||
|
||||
.num_storage_buffers = 1,
|
||||
.num_uniform_buffers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUVertexAttribute vertex_attributes[] = {
|
||||
{
|
||||
.location = 0,
|
||||
.buffer_slot = 0,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = 0,
|
||||
},
|
||||
{
|
||||
.location = 1,
|
||||
.buffer_slot = 0,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
|
||||
.offset = 16,
|
||||
},
|
||||
{
|
||||
.location = 2,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = 0,
|
||||
},
|
||||
{
|
||||
.location = 3,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_UINT,
|
||||
.offset = 16,
|
||||
},
|
||||
{
|
||||
.location = 4,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = 20,
|
||||
},
|
||||
{
|
||||
.location = 5,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = 36,
|
||||
},
|
||||
SDL_GPUShader *world_vertex_shader = SDL_CreateGPUShader(device, &world_vertex_shader_info);
|
||||
if (!world_vertex_shader) {
|
||||
log_error("Failed to create world vertex shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUShaderCreateInfo world_pixel_shader_info = {
|
||||
.code_size = SDL_arraysize(SPIRV_world),
|
||||
.code = SPIRV_world,
|
||||
.entrypoint = "main_fragment",
|
||||
.format = SDL_GPU_SHADERFORMAT_SPIRV,
|
||||
.stage = SDL_GPU_SHADERSTAGE_FRAGMENT,
|
||||
|
||||
.num_samplers = 1,
|
||||
};
|
||||
|
||||
SDL_GPUShader *world_pixel_shader = SDL_CreateGPUShader(device, &world_pixel_shader_info);
|
||||
if (!world_pixel_shader) {
|
||||
log_error("Failed to create world pixel shader. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_GPUColorTargetDescription color_target_descriptions[] = {
|
||||
{
|
||||
.format = SDL_GetGPUSwapchainTextureFormat(device, window),
|
||||
@@ -691,40 +704,140 @@ int main(int argc, char **argv) {
|
||||
},
|
||||
};
|
||||
|
||||
SDL_GPUGraphicsPipelineCreateInfo basic_graphics_pipeline_info = {
|
||||
.vertex_shader = basic_vertex_shader,
|
||||
.fragment_shader = basic_pixel_shader,
|
||||
.vertex_input_state = {
|
||||
.vertex_buffer_descriptions = vertex_buffer_descriptions,
|
||||
.num_vertex_buffers = SDL_arraysize(vertex_buffer_descriptions),
|
||||
.vertex_attributes = vertex_attributes,
|
||||
.num_vertex_attributes = SDL_arraysize(vertex_attributes),
|
||||
},
|
||||
.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST,
|
||||
.rasterizer_state = {
|
||||
.fill_mode = SDL_GPU_FILLMODE_FILL,
|
||||
.cull_mode = SDL_GPU_CULLMODE_BACK,
|
||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||
},
|
||||
.target_info = {
|
||||
.color_target_descriptions = color_target_descriptions,
|
||||
.num_color_targets = SDL_arraysize(color_target_descriptions),
|
||||
},
|
||||
};
|
||||
{
|
||||
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||
{
|
||||
.slot = 0,
|
||||
.pitch = sizeof(Vertex),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
|
||||
},
|
||||
{
|
||||
.slot = 1,
|
||||
.pitch = sizeof(Instance),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_INSTANCE,
|
||||
.instance_step_rate = 1,
|
||||
},
|
||||
};
|
||||
|
||||
basic_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &basic_graphics_pipeline_info);
|
||||
if (!basic_graphics_pipeline) {
|
||||
log_error("Failed to create basic graphics pipeline. Exiting.");
|
||||
return 1;
|
||||
SDL_GPUVertexAttribute vertex_attributes[] = {
|
||||
{
|
||||
.location = 0,
|
||||
.buffer_slot = 0,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
|
||||
.offset = offsetof(Vertex, pos),
|
||||
},
|
||||
{
|
||||
.location = 1,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
|
||||
.offset = offsetof(Instance, pos),
|
||||
},
|
||||
{
|
||||
.location = 2,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = offsetof(Instance, uv0uv1),
|
||||
},
|
||||
{
|
||||
.location = 3,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4,
|
||||
.offset = offsetof(Instance, uv2uv3),
|
||||
},
|
||||
};
|
||||
|
||||
SDL_GPUGraphicsPipelineCreateInfo basic_graphics_pipeline_info = {
|
||||
.vertex_shader = basic_vertex_shader,
|
||||
.fragment_shader = basic_pixel_shader,
|
||||
.vertex_input_state = {
|
||||
.vertex_buffer_descriptions = vertex_buffer_descriptions,
|
||||
.num_vertex_buffers = SDL_arraysize(vertex_buffer_descriptions),
|
||||
.vertex_attributes = vertex_attributes,
|
||||
.num_vertex_attributes = SDL_arraysize(vertex_attributes),
|
||||
},
|
||||
.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST,
|
||||
.rasterizer_state = {
|
||||
.fill_mode = SDL_GPU_FILLMODE_FILL,
|
||||
.cull_mode = SDL_GPU_CULLMODE_BACK,
|
||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||
.enable_depth_clip = true,
|
||||
},
|
||||
.target_info = {
|
||||
.color_target_descriptions = color_target_descriptions,
|
||||
.num_color_targets = SDL_arraysize(color_target_descriptions),
|
||||
},
|
||||
};
|
||||
|
||||
basic_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &basic_graphics_pipeline_info);
|
||||
if (!basic_graphics_pipeline) {
|
||||
log_error("Failed to create basic graphics pipeline. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_ReleaseGPUShader(device, basic_vertex_shader);
|
||||
SDL_ReleaseGPUShader(device, basic_pixel_shader);
|
||||
}
|
||||
|
||||
for (int y = 0; y < view_height; y++) {
|
||||
for (int x = 0; x < view_width; x++) {
|
||||
tiles_instances[x + y * view_width].pos_size.zw = { 1.0f / view_width, 1.0f / view_height };
|
||||
tiles_instances[x + y * view_width].pos_size.xy = { (float)x / view_width, (float)y / (float)view_height };
|
||||
tiles_instances[x + y * view_width].pos_size.xy += tiles_instances[x + y * view_width].pos_size.zw * 0.5f;
|
||||
tiles_instances[x + y * view_width].uv0uv1 = { 0, 0, 1, 1 };
|
||||
{
|
||||
SDL_GPUVertexBufferDescription vertex_buffer_descriptions[] = {
|
||||
{
|
||||
.slot = 0,
|
||||
.pitch = sizeof(Vertex),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
|
||||
},
|
||||
{
|
||||
.slot = 1,
|
||||
.pitch = sizeof(Uint32),
|
||||
.input_rate = SDL_GPU_VERTEXINPUTRATE_INSTANCE,
|
||||
.instance_step_rate = 1,
|
||||
},
|
||||
};
|
||||
|
||||
SDL_GPUVertexAttribute vertex_attributes[] = {
|
||||
{
|
||||
.location = 0,
|
||||
.buffer_slot = 0,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3,
|
||||
.offset = offsetof(Vertex, pos),
|
||||
},
|
||||
{
|
||||
.location = 1,
|
||||
.buffer_slot = 1,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_UINT,
|
||||
.offset = 0,
|
||||
},
|
||||
};
|
||||
|
||||
SDL_GPUGraphicsPipelineCreateInfo world_graphics_pipeline_info = {
|
||||
.vertex_shader = world_vertex_shader,
|
||||
.fragment_shader = world_pixel_shader,
|
||||
.vertex_input_state = {
|
||||
.vertex_buffer_descriptions = vertex_buffer_descriptions,
|
||||
.num_vertex_buffers = SDL_arraysize(vertex_buffer_descriptions),
|
||||
.vertex_attributes = vertex_attributes,
|
||||
.num_vertex_attributes = SDL_arraysize(vertex_attributes),
|
||||
},
|
||||
.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST,
|
||||
.rasterizer_state = {
|
||||
.fill_mode = SDL_GPU_FILLMODE_FILL,
|
||||
.cull_mode = SDL_GPU_CULLMODE_BACK,
|
||||
.front_face = SDL_GPU_FRONTFACE_CLOCKWISE,
|
||||
.enable_depth_clip = true,
|
||||
},
|
||||
.target_info = {
|
||||
.color_target_descriptions = color_target_descriptions,
|
||||
.num_color_targets = SDL_arraysize(color_target_descriptions),
|
||||
},
|
||||
};
|
||||
|
||||
world_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(device, &world_graphics_pipeline_info);
|
||||
if (!world_graphics_pipeline) {
|
||||
log_error("Failed to create world graphics pipeline. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_ReleaseGPUShader(device, world_vertex_shader);
|
||||
SDL_ReleaseGPUShader(device, world_pixel_shader);
|
||||
}
|
||||
|
||||
SDL_GPUTexture *player_texture = create_shader_texture("../assets/decorations/strawberry.png");
|
||||
@@ -733,6 +846,7 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tile_border_size = 1;
|
||||
tile_atlas = sma_atlas_create(tile_atlas_size, tile_atlas_size);
|
||||
char *tile_atlas_texture_cpu = (char *)calloc(1, tile_atlas_size * tile_atlas_size * 4);
|
||||
for (int i = 0; i < SDL_arraysize(tile_infos); i++) {
|
||||
@@ -743,15 +857,40 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
tile_infos[i].atlas_item = sma_item_add(tile_atlas, width, height);
|
||||
tile_infos[i].atlas_item = sma_item_add(tile_atlas, width + 2 * tile_border_size, height + 2 * tile_border_size);
|
||||
if (!tile_infos[i].atlas_item) {
|
||||
log_error("Failed to add tile texture to atlas. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
int item_x = sma_item_x(tile_infos[i].atlas_item);
|
||||
int item_y = sma_item_y(tile_infos[i].atlas_item);
|
||||
|
||||
tile_infos[i].uv_min = { .x = (item_x + tile_border_size) / (float)tile_atlas_size, .y = (item_y + tile_border_size) / (float)tile_atlas_size };
|
||||
tile_infos[i].uv_max = { .x = (item_x + tile_border_size + width) / (float)tile_atlas_size, .y = (item_y + tile_border_size + height) / (float)tile_atlas_size };
|
||||
|
||||
cpu_tile_infos_buffer[i] = V4{ tile_infos[i].uv_min, tile_infos[i].uv_max };
|
||||
|
||||
blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + tile_border_size, item_y + tile_border_size, (char *)data, width, width, height);
|
||||
|
||||
// EDGES
|
||||
|
||||
/* TOP */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + 1, item_y + 0, (char *)data, width, width, 1);
|
||||
/* BOTTOM */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + 1, item_y + height + 1, (char *)data + width * (height - 1) * 4, width, width, 1);
|
||||
|
||||
/* LEFT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + 0, item_y + 1, (char *)data, width, 1, height);
|
||||
/* RIGHT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + width + 1, item_y + 1, (char *)data + (width - 1) * 4, width, 1, height);
|
||||
|
||||
// CORNERS
|
||||
|
||||
/* TOP-LEFT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x, item_y, (char *)data, width, 1, 1);
|
||||
/* TOP-RIGHT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + width + 1, item_y, (char *)data + (width - 1) * 4, width, 1, 1);
|
||||
|
||||
/* BOTTOM-LEFT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x, item_y + height + 1, (char *)data + (height - 1) * width * 4, width, 1, 1);
|
||||
/* BOTTOM-RIGHT */ blit(tile_atlas_texture_cpu, tile_atlas_size, item_x + width + 1, item_y + height + 1, (char *)data + (height - 1) * width * 4 + (width -1 ) * 4, width, 1, 1);
|
||||
|
||||
blit(tile_atlas_texture_cpu, tile_atlas_size, sma_item_x(tile_infos[i].atlas_item), sma_item_y(tile_infos[i].atlas_item), (char *)data, width, height);
|
||||
stbi_image_free(data);
|
||||
}
|
||||
|
||||
SDL_GPUTexture *tile_atlas_texture = create_shader_texture("tile_atlas", tile_atlas_texture_cpu, tile_atlas_size, tile_atlas_size, 4);
|
||||
free(tile_atlas_texture_cpu);
|
||||
if (!tile_atlas_texture) {
|
||||
@@ -767,9 +906,13 @@ int main(int argc, char **argv) {
|
||||
.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
|
||||
.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
|
||||
.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
|
||||
|
||||
.max_anisotropy = 16.0f,
|
||||
.enable_anisotropy = true,
|
||||
};
|
||||
|
||||
point_sampler = SDL_CreateGPUSampler(device, &point_sampler_info);
|
||||
SDL_GPUTextureSamplerBinding tile_atlas_texture_binding = { .texture = tile_atlas_texture, .sampler = point_sampler };
|
||||
|
||||
vertex_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices), vertices, "vertex_buffer");
|
||||
if (!vertex_buffer) {
|
||||
@@ -783,28 +926,39 @@ int main(int argc, char **argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
tiles_instance_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(tiles_instances), tiles_instances, "tiles_instance_buffer");
|
||||
if (!tiles_instance_buffer) {
|
||||
log_error("Failed to create buffer. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
player_instance_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(player_instance), &player_instance, "player_instance_buffer");
|
||||
if (!player_instance_buffer) {
|
||||
log_error("Failed to create buffer. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
load_map();
|
||||
world_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, map_width * map_height * 4, map_tiles, "world_buffer");
|
||||
if (!world_buffer) {
|
||||
log_error("Failed to create buffer. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
tile_infos_buffer = create_buffer(SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ, SDL_arraysize(cpu_tile_infos_buffer) * sizeof(*cpu_tile_infos_buffer), cpu_tile_infos_buffer, "tile_infos_buffer");
|
||||
if (!tile_infos_buffer) {
|
||||
log_error("Failed to create buffer. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiContext *imgui_context = ImGui::CreateContext();
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_IsSRGB;
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
if (io.ConfigFlags & ImGuiConfigFlags_IsSRGB){
|
||||
for (int i = 0; i < IM_ARRAYSIZE(imgui_context->Style.Colors); i++) {
|
||||
imgui_context->Style.Colors[i] = sRGB_to_linear(imgui_context->Style.Colors[i]);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui_ImplSDL3_InitForSDLGPU(window);
|
||||
ImGui_ImplSDLGPU3_InitInfo imgui_init_info = {
|
||||
.Device = device,
|
||||
@@ -816,7 +970,6 @@ int main(int argc, char **argv) {
|
||||
bool show_demo_window = true;
|
||||
bool first_frame = true;
|
||||
|
||||
|
||||
SDL_GetWindowSizeInPixels(window, &window_width, &window_height);
|
||||
|
||||
// MSG Message;
|
||||
@@ -833,7 +986,6 @@ int main(int argc, char **argv) {
|
||||
selected_tile = -1;
|
||||
}
|
||||
|
||||
SDL_GPUTextureSamplerBinding texture_binding = { .texture = tile_atlas_texture, .sampler = point_sampler };
|
||||
for (int i = 0; i < SDL_arraysize(tile_infos); i++) {
|
||||
ImGui::PushID(i);
|
||||
|
||||
@@ -844,7 +996,7 @@ int main(int argc, char **argv) {
|
||||
if (available >= 32)
|
||||
ImGui::SameLine();
|
||||
|
||||
if (SelectableImage("##tile", selected_tile == i, &texture_binding, ImVec2(32, 32), uv0, uv1, SDL_max(selected_rotation, 0)))
|
||||
if (SelectableImage("##tile", selected_tile == i, &tile_atlas_texture_binding, ImVec2(32, 32), uv0, uv1, SDL_max(selected_rotation, 0)))
|
||||
selected_tile = i;
|
||||
|
||||
ImGui::PopID();
|
||||
@@ -856,30 +1008,33 @@ int main(int argc, char **argv) {
|
||||
ImVec2 uv0 = ImVec2(sma_item_x(tile_infos[selected_tile].atlas_item) / (float)tile_atlas_size, sma_item_y(tile_infos[selected_tile].atlas_item) / (float)tile_atlas_size);
|
||||
ImVec2 uv1 = ImVec2(uv0.x + sma_item_width(tile_infos[selected_tile].atlas_item) / (float)tile_atlas_size, uv0.y + sma_item_height(tile_infos[selected_tile].atlas_item) / (float)tile_atlas_size);
|
||||
|
||||
if (SelectableImage("##None", selected_rotation == 0, &texture_binding, ImVec2(32, 32), uv0, uv1, 0))
|
||||
if (SelectableImage("##None", selected_rotation == 0, &tile_atlas_texture_binding, ImVec2(32, 32), uv0, uv1, 0))
|
||||
selected_rotation = 0;
|
||||
|
||||
if (ImGui::GetContentRegionAvail().x - ImGui::GetItemRectMax().x >= 32)
|
||||
ImGui::SameLine();
|
||||
|
||||
if (SelectableImage("##90", selected_rotation == 1, &texture_binding, ImVec2(32, 32), uv0, uv1, 1))
|
||||
if (SelectableImage("##90", selected_rotation == 1, &tile_atlas_texture_binding, ImVec2(32, 32), uv0, uv1, 1))
|
||||
selected_rotation = 1;
|
||||
|
||||
if (ImGui::GetContentRegionAvail().x - ImGui::GetItemRectMax().x >= 32)
|
||||
ImGui::SameLine();
|
||||
|
||||
if (SelectableImage("##180", selected_rotation == 2, &texture_binding, ImVec2(32, 32), uv0, uv1, 2))
|
||||
if (SelectableImage("##180", selected_rotation == 2, &tile_atlas_texture_binding, ImVec2(32, 32), uv0, uv1, 2))
|
||||
selected_rotation = 2;
|
||||
|
||||
if (ImGui::GetContentRegionAvail().x - ImGui::GetItemRectMax().x >= 32)
|
||||
ImGui::SameLine();
|
||||
|
||||
if (SelectableImage("##270", selected_rotation == 3, &texture_binding, ImVec2(32, 32), uv0, uv1, 3))
|
||||
if (SelectableImage("##270", selected_rotation == 3, &tile_atlas_texture_binding, ImVec2(32, 32), uv0, uv1, 3))
|
||||
selected_rotation = 3;
|
||||
|
||||
if (ImGui::Selectable("Random", selected_rotation == -1))
|
||||
selected_rotation = -1;
|
||||
}
|
||||
|
||||
ImGui::DragFloat("fovy", &per_frame.fovy_degrees);
|
||||
ImGui::DragFloat("camera_z", &per_frame.camera_z);
|
||||
}
|
||||
ImGui::End();
|
||||
|
||||
@@ -955,31 +1110,12 @@ int main(int argc, char **argv) {
|
||||
float mouse_x = event.button.x;
|
||||
float mouse_y = event.button.y;
|
||||
|
||||
V2 X = V2{ -1, 1 };
|
||||
V2 Y = V2{ -1, 1 };
|
||||
V2 mouse = remap(V2{ 0, 0 }, V2{ (float)window_width, (float)window_height }, V2{ -1, -1 }, V2{ 1, 1 }, V2{ mouse_x, mouse_y });
|
||||
V4 mouse_world = inverse_projection_matrix * V4{ mouse.x, mouse.y, 10 * 0.001, 1 };
|
||||
mouse_world.xy *= mouse_world.w;
|
||||
|
||||
float correction_factor = (window_width / (float)window_height) / (16.0 / 9.0);
|
||||
if (correction_factor < 1)
|
||||
Y *= correction_factor;
|
||||
else
|
||||
X /= correction_factor;
|
||||
|
||||
float m_x = remap(0, window_width, -1, 1, mouse_x);
|
||||
float m_y = remap(0, window_height, -1, 1, mouse_y);
|
||||
|
||||
mouse_x = remap(X.x, X.y, 0, window_width, m_x);
|
||||
mouse_y = remap(Y.x, Y.y, 0, window_height, m_y);
|
||||
|
||||
float tile_width = window_width / (float) view_width;
|
||||
float tile_height = window_height / (float) view_height;
|
||||
|
||||
int tile_x = (int)(mouse_x / tile_width) + player.pos_x - (view_width / 2);
|
||||
int tile_y = (int)(mouse_y / tile_height) + player.pos_y - (view_height / 2);
|
||||
|
||||
if (mouse_x < 0)
|
||||
tile_x = -1;
|
||||
if (mouse_y< 0)
|
||||
tile_y = -1;
|
||||
Sint32 tile_x = roundf(mouse_world.x) + player.pos_x;
|
||||
Sint32 tile_y = roundf(mouse_world.y) + player.pos_y;
|
||||
|
||||
if (selected_tile != -1) {
|
||||
if(0 <= tile_x && tile_x < map_width &&
|
||||
@@ -990,11 +1126,13 @@ int main(int argc, char **argv) {
|
||||
} else {
|
||||
map_tiles[tile_x + map_width * tile_y] = ((selected_rotation & 3) << 16) | selected_tile;
|
||||
}
|
||||
|
||||
update_buffer(world_buffer, (tile_x + map_width * tile_y) * sizeof(Uint32), sizeof(Uint32), &map_tiles[tile_x + map_width * tile_y]);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Keymod modifiers = SDL_GetModState();
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_x == -1) {
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_x <= -1) {
|
||||
if(modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size('W', -1);
|
||||
else
|
||||
@@ -1008,7 +1146,7 @@ int main(int argc, char **argv) {
|
||||
change_map_size('E', 1);
|
||||
}
|
||||
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_y == -1) {
|
||||
if (modifiers & SDL_KMOD_SHIFT && tile_y <= -1) {
|
||||
if (modifiers & SDL_KMOD_CTRL)
|
||||
change_map_size('N', -1);
|
||||
else
|
||||
@@ -1025,54 +1163,27 @@ int main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
//Tiles updaten
|
||||
for (int y = 0; y < view_height; y++) {
|
||||
for (int x = 0; x < view_width; x++) {
|
||||
Uint8 orientation = 0;
|
||||
|
||||
if (x + player.pos_x - view_width / 2 >= 0 && x + player.pos_x - view_width / 2 < map_width &&
|
||||
y + player.pos_y - view_height / 2 >= 0 && y + player.pos_y - view_height / 2 < map_height) {
|
||||
tiles_instances[x + y * view_width].tile_type = map_tiles[(x + player.pos_x - view_width / 2) + (y + player.pos_y - view_height / 2) * map_width] & 0xffff;
|
||||
orientation = (map_tiles[(x + player.pos_x - view_width / 2) + (y + player.pos_y - view_height / 2) * map_width] & 0x00030000) >> 16;
|
||||
} else {
|
||||
tiles_instances[x + y * view_width].tile_type = 0;
|
||||
}
|
||||
|
||||
Uint32 type = tiles_instances[x + y * view_width].tile_type;
|
||||
|
||||
float x0 = sma_item_x(tile_infos[type].atlas_item);
|
||||
float y0 = sma_item_y(tile_infos[type].atlas_item);
|
||||
float x1 = x0 + sma_item_width(tile_infos[type].atlas_item);
|
||||
float y1 = y0 + sma_item_height(tile_infos[type].atlas_item);
|
||||
|
||||
x0 /= tile_atlas_size;
|
||||
y0 /= tile_atlas_size;
|
||||
x1 /= tile_atlas_size;
|
||||
y1 /= tile_atlas_size;
|
||||
|
||||
switch (orientation) {
|
||||
case 0: tiles_instances[x + y * view_width].uv0uv1 = { x0 , y0, x1, y0 }; tiles_instances[x + y * view_width].uv2uv3 = { x1 , y1, x0, y1 }; break;
|
||||
case 1: tiles_instances[x + y * view_width].uv0uv1 = { x1 , y0, x1, y1 }; tiles_instances[x + y * view_width].uv2uv3 = { x0 , y1, x0, y0 }; break;
|
||||
case 2: tiles_instances[x + y * view_width].uv0uv1 = { x1 , y1, x0, y1 }; tiles_instances[x + y * view_width].uv2uv3 = { x0 , y0, x1, y0 }; break;
|
||||
case 3: tiles_instances[x + y * view_width].uv0uv1 = { x0 , y1, x0, y0 }; tiles_instances[x + y * view_width].uv2uv3 = { x1 , y0, x1, y1 }; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!swapchain_texture) {
|
||||
SDL_CancelGPUCommandBuffer(command_buffer);
|
||||
ImGui::Render();
|
||||
ImDrawData *draw_data = ImGui::GetDrawData();
|
||||
continue;
|
||||
}
|
||||
|
||||
player_instance.pos.x = player.pos_x;
|
||||
player_instance.pos.y = player.pos_y;
|
||||
|
||||
update_buffer(player_instance_buffer, 0, sizeof(player_instance), &player_instance);
|
||||
|
||||
per_frame.aspect_ratio = ((float) window_width / (float) window_height);
|
||||
per_frame.map_width = map_width;
|
||||
per_frame.camera_x = player.pos_x;
|
||||
per_frame.camera_y = player.pos_y;
|
||||
|
||||
per_frame.aspect_ratio = (float) window_width / (float) window_height;
|
||||
SDL_PushGPUVertexUniformData(command_buffer, 0, &per_frame, sizeof(per_frame));
|
||||
|
||||
if (!update_buffer(tiles_instance_buffer, 0, sizeof(tiles_instances), tiles_instances)) {
|
||||
log_error("Failed to update buffer. Exiting.");
|
||||
return 1;
|
||||
}
|
||||
projection_matrix = projection (radians(per_frame.fovy_degrees), per_frame.aspect_ratio, 0.001f);
|
||||
inverse_projection_matrix = inverse_projection(radians(per_frame.fovy_degrees), per_frame.aspect_ratio, 0.001f);
|
||||
|
||||
SDL_GPUColorTargetInfo color_target_info = {
|
||||
.texture = swapchain_texture,
|
||||
@@ -1090,18 +1201,19 @@ int main(int argc, char **argv) {
|
||||
{ // Draw Map
|
||||
SDL_GPUBufferBinding index_buffer_binding = { .buffer = index_buffer, .offset = 0 };
|
||||
SDL_GPUBufferBinding vertex_buffers[] = {
|
||||
{ .buffer = vertex_buffer, .offset = 0 },
|
||||
{ .buffer = tiles_instance_buffer, .offset = 0 },
|
||||
{ .buffer = vertex_buffer, .offset = 0 },
|
||||
{ .buffer = world_buffer, .offset = 0 },
|
||||
};
|
||||
SDL_GPUTextureSamplerBinding texture_bindings[] = {
|
||||
{ .texture = tile_atlas_texture, .sampler = point_sampler },
|
||||
};
|
||||
|
||||
SDL_BindGPUGraphicsPipeline(render_pass, basic_graphics_pipeline);
|
||||
SDL_BindGPUGraphicsPipeline(render_pass, world_graphics_pipeline);
|
||||
SDL_BindGPUIndexBuffer(render_pass, &index_buffer_binding, SDL_GPU_INDEXELEMENTSIZE_16BIT);
|
||||
SDL_BindGPUVertexBuffers(render_pass, 0, vertex_buffers, SDL_arraysize(vertex_buffers));
|
||||
SDL_BindGPUVertexStorageBuffers(render_pass, 0, &tile_infos_buffer, 1);
|
||||
SDL_BindGPUFragmentSamplers(render_pass, 0, texture_bindings, SDL_arraysize(texture_bindings));
|
||||
SDL_DrawGPUIndexedPrimitives(render_pass, 6, view_width * view_height, 0, 0, 0);
|
||||
SDL_DrawGPUIndexedPrimitives(render_pass, 6, map_height * map_width, 0, 0, 0);
|
||||
}
|
||||
|
||||
{ // Draw Player
|
||||
|
||||
+812
-759
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user