Getting texture atlas to animate
This commit is contained in:
parent
e1e41654e6
commit
dc054cacfb
14
main.c
14
main.c
@ -6,6 +6,7 @@
|
|||||||
#include "cglm/cglm.h"
|
#include "cglm/cglm.h"
|
||||||
#include "lib.h"
|
#include "lib.h"
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
|
#include "rendering.h"
|
||||||
#include "libcyaml/include/cyaml/cyaml.h"
|
#include "libcyaml/include/cyaml/cyaml.h"
|
||||||
|
|
||||||
#define PI 3.14159f
|
#define PI 3.14159f
|
||||||
@ -18,6 +19,7 @@ typedef struct State {
|
|||||||
float camY;
|
float camY;
|
||||||
float px;
|
float px;
|
||||||
float py;
|
float py;
|
||||||
|
int spriteY;
|
||||||
} State;
|
} State;
|
||||||
|
|
||||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||||
@ -33,6 +35,12 @@ void processInput(GLFWwindow *window, State* state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float amount = 1.0f;
|
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) {
|
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
|
||||||
state->camY -= amount;
|
state->camY -= amount;
|
||||||
}
|
}
|
||||||
@ -120,7 +128,8 @@ int main(void) {
|
|||||||
|
|
||||||
unsigned int textureID;
|
unsigned int textureID;
|
||||||
int width, height, channels;
|
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) {
|
if (data == NULL) {
|
||||||
fprintf(stderr, "Could not load texture\n");
|
fprintf(stderr, "Could not load texture\n");
|
||||||
return -1;
|
return -1;
|
||||||
@ -205,13 +214,12 @@ int main(void) {
|
|||||||
glUniform1f(location, spriteX);
|
glUniform1f(location, spriteX);
|
||||||
|
|
||||||
location = glGetUniformLocation(shaderProgram, "spriteY");
|
location = glGetUniformLocation(shaderProgram, "spriteY");
|
||||||
glUniform1f(location, state.py);
|
glUniform1f(location, state.camY);
|
||||||
|
|
||||||
glUniform1i(glGetUniformLocation(shaderProgram, "image"), 0);
|
glUniform1i(glGetUniformLocation(shaderProgram, "image"), 0);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, textureID);
|
glBindTexture(GL_TEXTURE_2D, textureID);
|
||||||
|
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
|
@ -8,7 +8,17 @@ uniform float spriteX;
|
|||||||
uniform float spriteY;
|
uniform float spriteY;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
float x = TexCoords.x / 10 + spriteX;
|
float r = (48.0 / 512.0);
|
||||||
float y = TexCoords.y;
|
float x = TexCoords.x * r + (int(spriteX * 10)) * r;
|
||||||
color = vec4(spriteColor, 1.0f) * texture(image, vec2(x, y));
|
// 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,14 +176,6 @@ int sdlTest(Image* images, int image_count, TexRect* tex_rects) {
|
|||||||
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
SDL_RenderPresent(renderer);
|
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_DestroyTexture(texture);
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
@ -250,6 +242,7 @@ int main(int argc, char* argv[]) {
|
|||||||
for (int i = 0; i < imageCount; i++) {
|
for (int i = 0; i < imageCount; i++) {
|
||||||
stbi_image_free(images[i].imageData);
|
stbi_image_free(images[i].imageData);
|
||||||
}
|
}
|
||||||
|
free(buf);
|
||||||
free(images);
|
free(images);
|
||||||
free(texRects);
|
free(texRects);
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user