Compare commits

..

No commits in common. "944c00b89e0d5120974dfb29ed6422420af9616b" and "cdf6cf00a805fa1a3ade4b2167a36a274988ead6" have entirely different histories.

View File

@ -372,6 +372,110 @@ static void change_map_size(char direction, int amount) {
SDL_ReleaseGPUBuffer(device, old_world_buffer); SDL_ReleaseGPUBuffer(device, old_world_buffer);
} }
static 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);
if (!data) {
log_error("Failed to load texture (\"%s\").", path);
return NULL;
}
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_SRGB;
if (format == SDL_GPU_TEXTUREFORMAT_INVALID) {
log_error("Failed to find texture format for texture (\"%s\").", path);
stbi_image_free(data);
return NULL;
}
SDL_PropertiesID properties = SDL_CreateProperties();
if (properties) SDL_SetStringProperty(properties, SDL_PROP_GPU_TEXTURE_CREATE_NAME_STRING, path);
SDL_GPUTextureCreateInfo texture_info = {
.type = SDL_GPU_TEXTURETYPE_2D,
.format = format,
.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
.width = (Uint32)width,
.height = (Uint32)height,
.layer_count_or_depth = 1,
.num_levels = 1,
.props = properties,
};
SDL_GPUTexture *texture = SDL_CreateGPUTexture(device, &texture_info);
if (!texture) {
log_error("Failed to create gpu texture (%s).", SDL_GetError());
stbi_image_free(data);
return NULL;
}
if (properties) SDL_DestroyProperties(properties);
Uint32 upload_size = width * height * channels;
SDL_GPUTransferBufferCreateInfo transfer_buffer_info = {
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = upload_size,
};
SDL_GPUTransferBuffer *transfer_buffer = SDL_CreateGPUTransferBuffer(device, &transfer_buffer_info);
if (!transfer_buffer) {
log_error("Failed to create gpu transfer buffer (%s).", SDL_GetError());
SDL_ReleaseGPUTexture(device, texture);
stbi_image_free(data);
return NULL;
}
void *transfer_data = SDL_MapGPUTransferBuffer(device, transfer_buffer, false);
if (!transfer_data) {
log_error("Failed to map gpu transfer buffer (%s).", SDL_GetError());
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
SDL_ReleaseGPUTexture(device, texture);
stbi_image_free(data);
return NULL;
}
memcpy(transfer_data, data, upload_size);
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
stbi_image_free(data);
SDL_GPUTextureTransferInfo transfer_info = {
.transfer_buffer = transfer_buffer,
.pixels_per_row = (Uint32)width,
};
SDL_GPUTextureRegion tetxure_region = {
.texture = texture,
.w = (Uint32)width,
.h = (Uint32)height,
.d = 1,
};
SDL_GPUCommandBuffer *command_buffer = SDL_AcquireGPUCommandBuffer(device);
if (!command_buffer) {
log_error("Failed to acquire gpu command buffer (%s).", SDL_GetError());
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
SDL_ReleaseGPUTexture(device, texture);
return NULL;
}
SDL_GPUCopyPass *copy_pass = SDL_BeginGPUCopyPass(command_buffer);
SDL_UploadToGPUTexture(copy_pass, &transfer_info, &tetxure_region, false);
SDL_EndGPUCopyPass(copy_pass);
if (!SDL_SubmitGPUCommandBuffer(command_buffer)) {
log_error("Failed to submit gpu command buffer (%s).", SDL_GetError());
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
SDL_ReleaseGPUTexture(device, texture);
SDL_CancelGPUCommandBuffer(command_buffer);
return NULL;
}
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
return texture;
}
static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) { static SDL_GPUTexture *create_shader_texture(const char *name, const char *data, int width, int height, int channels) {
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID; SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM; if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
@ -405,7 +509,6 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
} }
if (properties) SDL_DestroyProperties(properties); if (properties) SDL_DestroyProperties(properties);
if (data) {
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,
@ -462,30 +565,9 @@ static SDL_GPUTexture *create_shader_texture(const char *name, const char *data,
} }
SDL_ReleaseGPUTransferBuffer(device, transfer_buffer); SDL_ReleaseGPUTransferBuffer(device, transfer_buffer);
}
return texture; return texture;
} }
static 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);
if (!data) {
log_error("Failed to load texture (\"%s\").", path);
return NULL;
}
SDL_GPUTexture *result = create_shader_texture(path, (char *)data, width, height, channels);
if (!result) {
log_error("Failed to load texture (\"%s\").", path);
stbi_image_free(data);
return NULL;
}
stbi_image_free(data);
return result;
}
static 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) { static 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++) for (Sint32 y = 0; y < height; y++)
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components); memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
@ -586,8 +668,8 @@ static void setup_memory_functions() {
static void setup_memory_functions() {} static void setup_memory_functions() {}
#endif #endif
static bool enable_msaa = true; bool enable_msaa = true;
static SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1; SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1;
static bool recreate_graphics_pipelines() { static bool recreate_graphics_pipelines() {
SDL_GPUColorTargetDescription color_target_descriptions[] = { SDL_GPUColorTargetDescription color_target_descriptions[] = {
@ -1095,14 +1177,6 @@ int main(int argc, char **argv) {
msaa_texture = NULL; msaa_texture = NULL;
} }
} }
if (selected_tile != -1 && ImGui::IsWindowFocused() && ImGui::IsKeyPressed(ImGuiKey_R, false)) {
if (ImGui::IsKeyDown(ImGuiKey_LeftShift)) {
selected_rotation = (selected_rotation - 1) & 3;
} else {
selected_rotation = (selected_rotation + 1) & 3;
}
}
} }
ImGui::End(); ImGui::End();
@ -1151,8 +1225,6 @@ int main(int argc, char **argv) {
if (io.WantCaptureKeyboard) if (io.WantCaptureKeyboard)
continue; continue;
SDL_Keymod modifiers = SDL_GetModState();
if (event.key.key == SDLK_UP || event.key.key == SDLK_W) { if (event.key.key == SDLK_UP || event.key.key == SDLK_W) {
player.pos_y = clamp(0, player.pos_y + 1, map_height - 2); player.pos_y = clamp(0, player.pos_y + 1, map_height - 2);
} }
@ -1176,16 +1248,6 @@ int main(int argc, char **argv) {
if (event.key.key == SDLK_F4) { if (event.key.key == SDLK_F4) {
load_map(); load_map();
} }
if (event.key.key == SDLK_R) {
if (selected_tile != -1 && selected_rotation != -1) {
if (modifiers & SDL_KMOD_SHIFT) {
selected_rotation = (selected_rotation - 1) & 3;
} else {
selected_rotation = (selected_rotation + 1) & 3;
}
}
}
} break; } break;
case SDL_EVENT_MOUSE_BUTTON_DOWN: { case SDL_EVENT_MOUSE_BUTTON_DOWN: {