diff --git a/build.sh b/build.sh index c04770f..ee0dd38 100755 --- 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 diff --git a/res/main b/res/main index e77d361..059b97b 100755 Binary files a/res/main and b/res/main differ diff --git a/src/main.c b/src/main.c index 66e364c..faeaeeb 100644 --- a/src/main.c +++ b/src/main.c @@ -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()); diff --git a/src/neko.c b/src/neko.c index c403f6b..0f40fb7 100644 --- a/src/neko.c +++ b/src/neko.c @@ -1,5 +1,6 @@ #include "neko.h" #include "common.h" +#include 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 diff --git a/src/neko.h b/src/neko.h index 0a6e976..ea9768b 100644 --- a/src/neko.h +++ b/src/neko.h @@ -7,8 +7,9 @@ #include -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