Compare commits
3 Commits
cdf6cf00a8
...
944c00b89e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
944c00b89e | ||
|
|
5ac98b6203 | ||
|
|
fc88859eaf |
252
src/main.cpp
252
src/main.cpp
@ -372,110 +372,6 @@ static void change_map_size(char direction, int amount) {
|
||||
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) {
|
||||
SDL_GPUTextureFormat format = SDL_GPU_TEXTUREFORMAT_INVALID;
|
||||
if (channels == 1) format = SDL_GPU_TEXTUREFORMAT_A8_UNORM;
|
||||
@ -509,65 +405,87 @@ 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 = {
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = upload_size,
|
||||
};
|
||||
if (data) {
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
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);
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(transfer_data, data, upload_size);
|
||||
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(transfer_data, data, upload_size);
|
||||
SDL_UnmapGPUTransferBuffer(device, transfer_buffer);
|
||||
|
||||
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 *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) {
|
||||
for (Sint32 y = 0; y < height; y++)
|
||||
memmove(&dst[((dst_y + y) * dst_pitch + dst_x) * components], &src[y * src_pitch * components], width * components);
|
||||
@ -668,8 +586,8 @@ static void setup_memory_functions() {
|
||||
static void setup_memory_functions() {}
|
||||
#endif
|
||||
|
||||
bool enable_msaa = true;
|
||||
SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1;
|
||||
static bool enable_msaa = true;
|
||||
static SDL_GPUSampleCount highest_supported_sample_count = SDL_GPU_SAMPLECOUNT_1;
|
||||
|
||||
static bool recreate_graphics_pipelines() {
|
||||
SDL_GPUColorTargetDescription color_target_descriptions[] = {
|
||||
@ -1177,6 +1095,14 @@ int main(int argc, char **argv) {
|
||||
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();
|
||||
|
||||
@ -1225,6 +1151,8 @@ int main(int argc, char **argv) {
|
||||
if (io.WantCaptureKeyboard)
|
||||
continue;
|
||||
|
||||
SDL_Keymod modifiers = SDL_GetModState();
|
||||
|
||||
if (event.key.key == SDLK_UP || event.key.key == SDLK_W) {
|
||||
player.pos_y = clamp(0, player.pos_y + 1, map_height - 2);
|
||||
}
|
||||
@ -1248,6 +1176,16 @@ int main(int argc, char **argv) {
|
||||
if (event.key.key == SDLK_F4) {
|
||||
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;
|
||||
|
||||
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user