#include "neko.h"
#include "common.h"
+#include <math.h>
static SDL_Texture *texture = NULL;
static int texture_width = 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;
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);
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