]> git.ayabusa.dev Git - catzattack.git/commitdiff
added neko states, I will need to add animations
authorayabusa <lebgpub@gmail.com>
Mon, 22 Sep 2025 13:22:00 +0000 (15:22 +0200)
committerayabusa <lebgpub@gmail.com>
Mon, 22 Sep 2025 13:22:00 +0000 (15:22 +0200)
build.sh
res/main
src/main.c
src/neko.c
src/neko.h

index c04770f9e49682f2cff327bcab4862f8825fc550..ee0dd38d90b0bf116896f188ab4d6ac7ca8c5e04 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -2,7 +2,7 @@
 
 echo Building with ...
 echo gcc src/main.c -o res/main $(pkg-config --cflags --libs sdl3 x11 xfixes sdl3-image)
-gcc src/main.c src/neko.c -I src/ -o res/main $(pkg-config --cflags --libs sdl3 x11 xfixes sdl3-image)
+gcc src/main.c src/neko.c -I src/ -o res/main $(pkg-config --cflags --libs sdl3 x11 xfixes sdl3-image) -lm
 
 echo Running...
 DISPLAY=:1 ./res/main
index e77d36189d6c65961d251b91fbbfd8c8945caa8f..059b97b768f4dba4600c74caa795342516fb702e 100755 (executable)
Binary files a/res/main and b/res/main differ
index 66e364cc633ec7e8f6d1d6b4b75be19b490c5bbf..faeaeeb12880b662e1b81c1fabe680237d76159a 100644 (file)
@@ -98,7 +98,7 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
     make_window_clickthrough();
     set_override_redirect();
 
-    neko_init(renderer);
+    neko_init(renderer, window);
 
     /* Open the image file */
     SDL_asprintf(&gif_path, "%ssample.gif", SDL_GetBasePath());
index c403f6b9fbcb08ea4d6f5aa892d613518e5d9a29..0f40fb7f7fd6e0735006915047322170ad63e406 100644 (file)
@@ -1,5 +1,6 @@
 #include "neko.h"
 #include "common.h"
+#include <math.h>
 
 static SDL_Texture *texture = NULL;
 static int texture_width = 0;
@@ -7,8 +8,21 @@ static int texture_height = 0;
 
 static float catx = 200;
 static float caty = 200;
+static float speed = 5.0;
 
-int neko_init(SDL_Renderer *renderer){
+#define TIME_BEFORE_SPLEEP 3000
+#define TIME_BEFORE_RUNNING 1000
+
+enum CatState {
+    SLEEPING,
+    RUNNING,
+    WAKING_UP,
+    STAND_BY,
+} cat_state = RUNNING;
+
+static int state_timer;
+
+int neko_init(SDL_Renderer *renderer, SDL_Window *window){
     SDL_Surface *surface = NULL;
     char *neko_bmp_path = NULL;
     
@@ -38,22 +52,14 @@ int neko_init(SDL_Renderer *renderer){
 
     SDL_DestroySurface(surface);  /* done with this, the texture has a copy of the pixels now. */
 
+    SDL_AddTimer(50, move_cat, window);
 }
 
 void neko_render(SDL_Renderer *renderer, SDL_Window *window){
-    float mousex, mousey;
-    int windowx, windowy;
-    float diffx, diffy;
     SDL_FRect dst_rect;
 
-    SDL_GetGlobalMouseState(&mousex, &mousey);
-    SDL_GetWindowPosition(window, &windowx, &windowy);
-    diffx = catx-(mousex-(float)windowx);
-    diffy = caty-(mousey-(float)windowy);
-    SDL_Log("(%f, %f)", diffx, diffy);
-
-    dst_rect.x = catx+(diffx/diffy);
-    dst_rect.y = caty+(diffy/diffx);
+    dst_rect.x = catx;
+    dst_rect.y = caty;
     dst_rect.h = WINDOW_HEIGHT/10;
     dst_rect.w = WINDOW_HEIGHT/10;
     SDL_RenderTexture(renderer, texture, NULL, &dst_rect);
@@ -61,4 +67,51 @@ void neko_render(SDL_Renderer *renderer, SDL_Window *window){
 
 void neko_quit(){
     SDL_DestroyTexture(texture);
+}
+
+Uint32 move_cat(void *window, SDL_TimerID timerID, Uint32 interval){
+    float mousex, mousey;
+    float diffx, diffy, hypo;
+    int windowx, windowy;
+
+    SDL_GetGlobalMouseState(&mousex, &mousey);
+    SDL_GetWindowPosition(window, &windowx, &windowy);
+
+    diffx = catx-(mousex-(float)windowx);
+    diffy = caty-(mousey-(float)windowy);
+    hypo = sqrt(diffx*diffx + diffy*diffy); 
+    switch (cat_state)
+    {
+    case RUNNING:
+        if (hypo>50){
+            catx -= (diffx*speed/hypo);
+            caty -= (diffy*speed/hypo);
+        }else{
+            cat_state = STAND_BY;
+            state_timer = SDL_GetTicks();
+        }
+        break;
+    case STAND_BY:
+        if (hypo>50){
+            cat_state = RUNNING;
+        }else if (SDL_GetTicks()-TIME_BEFORE_SPLEEP > state_timer){
+            cat_state = SLEEPING;
+        }
+        break;
+    case SLEEPING:
+        if (hypo>50){
+            cat_state = WAKING_UP;
+            state_timer = SDL_GetTicks();
+        }
+        break;
+    case WAKING_UP:
+        if (SDL_GetTicks()-TIME_BEFORE_RUNNING > state_timer){
+            cat_state = RUNNING;
+        }
+        break;
+    
+    default:
+        break;
+    }
+    return(interval);
 }
\ No newline at end of file
index 0a6e976c6e5e9e24e5f7ed900fc3e6eadc7d9d73..ea9768bc1170e40d803703a4b87157ad8d2bb739 100644 (file)
@@ -7,8 +7,9 @@
 
 #include <stdio.h>
 
-int neko_init(SDL_Renderer *renderer);
+int neko_init(SDL_Renderer *renderer, SDL_Window *window);
 void neko_render(SDL_Renderer *renderer, SDL_Window *window);
 void neko_quit();
+Uint32 move_cat(void *window, SDL_TimerID timerID, Uint32 interval);
 
 #endif
\ No newline at end of file