added neko states, I will need to add animations
This commit is contained in:
2
build.sh
2
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
|
||||
|
@@ -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());
|
||||
|
77
src/neko.c
77
src/neko.c
@@ -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);
|
||||
@@ -62,3 +68,50 @@ 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);
|
||||
}
|
@@ -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
|
Reference in New Issue
Block a user