From dc054cacfb9a7017243f8447a29b89c4471d407f Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Wed, 25 Oct 2023 21:52:29 +0700 Subject: [PATCH] Getting texture atlas to animate --- main.c | 14 +++++++++++--- shaders/main.frag | 16 +++++++++++++--- tools/texturepacker.c | 9 +-------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/main.c b/main.c index be1f7e5..b181208 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include "cglm/cglm.h" #include "lib.h" #include "stb_image.h" +#include "rendering.h" #include "libcyaml/include/cyaml/cyaml.h" #define PI 3.14159f @@ -18,6 +19,7 @@ typedef struct State { float camY; float px; float py; + int spriteY; } State; void framebuffer_size_callback(GLFWwindow* window, int width, int height) { @@ -33,6 +35,12 @@ void processInput(GLFWwindow *window, State* state) { } float amount = 1.0f; + if (glfwGetKey(window, GLFW_KEY_J) == GLFW_RELEASE) { + state->spriteY -= 1; + } + if (glfwGetKey(window, GLFW_KEY_K) == GLFW_RELEASE) { + state->spriteY += 1; + } if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { state->camY -= amount; } @@ -120,7 +128,8 @@ int main(void) { unsigned int textureID; int width, height, channels; - char* data = (char*)stbi_load("assets/idle.png", &width, &height, &channels, 4); + char* data = (char*)stbi_load("assets/player.png", &width, &height, &channels, 4); + // char* data = (char*)stbi_load("imports/sprites/player/idle.png", &width, &height, &channels, 4); if (data == NULL) { fprintf(stderr, "Could not load texture\n"); return -1; @@ -205,13 +214,12 @@ int main(void) { glUniform1f(location, spriteX); location = glGetUniformLocation(shaderProgram, "spriteY"); - glUniform1f(location, state.py); + glUniform1f(location, state.camY); glUniform1i(glGetUniformLocation(shaderProgram, "image"), 0); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textureID); - glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); diff --git a/shaders/main.frag b/shaders/main.frag index 3c11306..72d9912 100644 --- a/shaders/main.frag +++ b/shaders/main.frag @@ -8,7 +8,17 @@ uniform float spriteX; uniform float spriteY; void main() { - float x = TexCoords.x / 10 + spriteX; - float y = TexCoords.y; - color = vec4(spriteColor, 1.0f) * texture(image, vec2(x, y)); + float r = (48.0 / 512.0); + float x = TexCoords.x * r + (int(spriteX * 10)) * r; + // float y = TexCoords.y * r + (int(spriteY * 10)) * r; + float y = TexCoords.y * r; + + float minb = 0.005; + float maxb = 0.995; + if (TexCoords.x <= minb || TexCoords.x >= maxb + || TexCoords.y <= minb || TexCoords.y >= maxb) { + color = vec4(1,0,0,1); + } else { + color = texture(image, vec2(x, y)); + } } diff --git a/tools/texturepacker.c b/tools/texturepacker.c index fe13b16..ed80ad8 100644 --- a/tools/texturepacker.c +++ b/tools/texturepacker.c @@ -176,14 +176,6 @@ int sdlTest(Image* images, int image_count, TexRect* tex_rects) { SDL_RenderCopy(renderer, texture, NULL, NULL); SDL_RenderPresent(renderer); } - // unsigned char converted[512 * 512 * 4]; - - // for (int i = 0; i < 512 * 512; i++) { - // converted[i*4+0] = (pixels[i] >> 24) & 0xff; - // converted[i*4+1] = (pixels[i] >> 16) & 0xff; - // converted[i*4+2] = (pixels[i] >> 8 ) & 0xff; - // converted[i*4+3] = (pixels[i] ) & 0xff; - // } SDL_DestroyTexture(texture); SDL_DestroyRenderer(renderer); @@ -250,6 +242,7 @@ int main(int argc, char* argv[]) { for (int i = 0; i < imageCount; i++) { stbi_image_free(images[i].imageData); } + free(buf); free(images); free(texRects); return 0;