Setting Window flags right; adding animation

This commit is contained in:
noffie 2025-03-16 15:24:31 +01:00
parent c0e3af64d6
commit 4536c62dad
2 changed files with 30 additions and 28 deletions

View file

@ -4,6 +4,11 @@
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
#ifdef _DEBUG
#include <stdio.h>
#endif
SpriteAnimation CreateSpriteAnimation(Texture2D texture, Vector2 sprite_texture_dimentions, Vector2 sprite_size, float rotation, float scale, int frameCount, float speed) SpriteAnimation CreateSpriteAnimation(Texture2D texture, Vector2 sprite_texture_dimentions, Vector2 sprite_size, float rotation, float scale, int frameCount, float speed)
{ {
SpriteAnimation spriteAnimation = malloc(sizeof(sprite_animation_t)); SpriteAnimation spriteAnimation = malloc(sizeof(sprite_animation_t));
@ -37,38 +42,29 @@ void UpdateSpriteAnimation(SpriteAnimation self, float dt) {
} }
void DrawSpriteAnimation(SpriteAnimation spriteAnimation, Vector2 position) { void DrawSpriteAnimation(SpriteAnimation spriteAnimation, Vector2 position) {
#ifdef _DEBUG position = Vector2Scale(position, .5f);
DrawCircleV(position, 5, DARKGREEN); float yscale = (spriteAnimation->size.y * spriteAnimation->scale) / 2;
#endif float xscale = (spriteAnimation->size.x * spriteAnimation->scale) / 2;
Rectangle destination = (Rectangle){
0,
0,
spriteAnimation->size.x * spriteAnimation->scale,
spriteAnimation->size.y * spriteAnimation->scale
} ;
Vector2 origin = (Vector2){ Rectangle destination = (Rectangle) {
.x = position.x + destination.width/2.f, xscale - position.x,
.y = position.y + destination.height/2.f yscale - position.y,
xscale + position.x,
yscale + position.y
}; };
#ifdef _DEBUG
DrawCircleV(origin, 5, RED);
#endif
DrawTexturePro( DrawTexturePro(
spriteAnimation->texture, spriteAnimation->texture,
(Rectangle){ spriteAnimation->sourceRect,
spriteAnimation->sourceRect.x,
spriteAnimation->sourceRect.y,
spriteAnimation->sourceRect.width,
spriteAnimation->sourceRect.height
},
destination, destination,
origin, (Vector2){0,0},
spriteAnimation->rotation, spriteAnimation->rotation,
WHITE WHITE
); );
#ifdef _DEBUG #ifdef _DEBUG
DrawCircleV(position, 5, DARKGREEN);
DrawRectangleLinesEx(destination, 2, BLACK); DrawRectangleLinesEx(destination, 2, BLACK);
#endif #endif
} }

View file

@ -19,11 +19,15 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
InitWindow(900, 600, "Colysis"); InitWindow(900, 600, "Colysis");
SetWindowState(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_MAXIMIZED);
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
Texture2D gery = LoadTexture(ASSETS_PATH "/karl-heinz_head.png"); Texture2D gery = LoadTexture(ASSETS_PATH "/karl-heinz_head.png");
SpriteAnimation geryanim = CreateSpriteAnimation(gery, (Vector2){64*14,64}, (Vector2){64,64}, 0.f, 4.f, 5, 0.15f); SpriteAnimation geryanim = CreateSpriteAnimation(gery,
(Vector2){64*14,64}, (Vector2){64,64},
0.f, 4.f, 5, 0.15f);
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.target = (Vector2){0,0}; camera.target = (Vector2){0,0};
@ -34,7 +38,7 @@ int main(int argc, char** argv)
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
ClearBackground((Color){ 0x00, 0x6d, 0xfb }); ClearBackground((Color){ 0x00, 0x6d, 0xfb });
float dt = GetFrameTime(); float dt = GetFrameTime();
camera.offset = (Vector2){GetScreenWidth()/2.f,GetScreenHeight()/2.f};
UpdateSpriteAnimation(geryanim, dt); UpdateSpriteAnimation(geryanim, dt);
@ -45,13 +49,13 @@ int main(int argc, char** argv)
BeginMode2D(camera); BeginMode2D(camera);
{ {
DrawSpriteAnimation(geryanim, (Vector2){0,0}); DrawSpriteAnimation(geryanim, (Vector2){0,0});
} }
EndMode2D(); EndMode2D();
{ {
DrawTextFull(GetFontDefault(), "Darling, I'm Home!", (Vector2){20,20}, TEXT_ORIENTATION_RIGHT, 0, 24, 5, 5, RAYWHITE); DrawTextFull(GetFontDefault(), "Darling, I'm Home!",
(Vector2){20,20}, TEXT_ORIENTATION_RIGHT,
0, 24, 5, 5, RAYWHITE);
} }
} }
@ -61,5 +65,7 @@ int main(int argc, char** argv)
DestroySpriteAnimation(geryanim); DestroySpriteAnimation(geryanim);
UnloadTexture(gery); UnloadTexture(gery);
CloseWindow();
return 0; return 0;
} }