very wonky movement implementation
This commit is contained in:
parent
d2155bf27e
commit
0fc07ac95e
6 changed files with 74 additions and 13 deletions
2
Makefile
2
Makefile
|
@ -4,7 +4,7 @@ PREFIX ?= /usr/local
|
||||||
CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3`
|
CFLAGS := $(CFLAGS) `pkg-config --cflags --libs sdl3`
|
||||||
LDFLAGS := $(LDFLAGS) -lm
|
LDFLAGS := $(LDFLAGS) -lm
|
||||||
|
|
||||||
SRCS = slutpet.c img.c samply.c
|
SRCS = slutpet.c img.c samply.c sdl.c
|
||||||
|
|
||||||
all: slutpet
|
all: slutpet
|
||||||
|
|
||||||
|
|
4
dat.h
4
dat.h
|
@ -3,6 +3,8 @@ typedef struct Image Image;
|
||||||
typedef struct State State;
|
typedef struct State State;
|
||||||
|
|
||||||
struct Samply {
|
struct Samply {
|
||||||
|
Image *tx; /* this is a pointer to one of the images below */
|
||||||
|
|
||||||
Image *idle;
|
Image *idle;
|
||||||
Image *walka;
|
Image *walka;
|
||||||
Image *walkb;
|
Image *walkb;
|
||||||
|
@ -21,6 +23,8 @@ struct Image {
|
||||||
struct State {
|
struct State {
|
||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_Renderer *ren;
|
SDL_Renderer *ren;
|
||||||
|
SDL_DisplayID dpy;
|
||||||
|
SDL_Rect bounds;
|
||||||
Uint64 lastframe;
|
Uint64 lastframe;
|
||||||
|
|
||||||
Samply *sam;
|
Samply *sam;
|
||||||
|
|
4
fns.h
4
fns.h
|
@ -8,4 +8,6 @@ Image *load_image(SDL_Renderer *ren, char *path);
|
||||||
|
|
||||||
int samply_step(State *st);
|
int samply_step(State *st);
|
||||||
void put_samply_to_bed(Samply *sam);
|
void put_samply_to_bed(Samply *sam);
|
||||||
Samply *wake_samply_up(SDL_Renderer *ren);
|
Samply *wake_samply_up(State *st);
|
||||||
|
|
||||||
|
int in_bounds(Image *i, int x, int y, SDL_Rect *bounds);
|
||||||
|
|
44
samply.c
44
samply.c
|
@ -7,15 +7,37 @@ int
|
||||||
samply_step(State *st)
|
samply_step(State *st)
|
||||||
{
|
{
|
||||||
Uint64 ms;
|
Uint64 ms;
|
||||||
SDL_Texture *tx;
|
int x, y;
|
||||||
|
|
||||||
|
if (SDL_GetWindowPosition(st->win, &x, &y) == false) {
|
||||||
|
shit("window pos query fail: %s", SDL_GetError());
|
||||||
|
goto render_it;
|
||||||
|
}
|
||||||
|
|
||||||
ms = SDL_GetTicks();
|
ms = SDL_GetTicks();
|
||||||
if ((ms / 100) % 2)
|
if ((ms / 100) % 2) {
|
||||||
tx = st->sam->walka->tex;
|
babble("anim walka");
|
||||||
else
|
st->sam->tx = st->sam->walka;
|
||||||
tx = st->sam->walkb->tex;
|
} else {
|
||||||
|
babble("anim walkb");
|
||||||
|
st->sam->tx = st->sam->walkb;
|
||||||
|
}
|
||||||
|
|
||||||
if (SDL_RenderTexture(st->ren, tx, NULL, NULL) == false) {
|
x++;
|
||||||
|
babble("bounds %d %d %d %d", st->bounds.x, st->bounds.y,
|
||||||
|
st->bounds.w, st->bounds.h);
|
||||||
|
babble("img %d %d %d %d", x, y, st->sam->tx->w, st->sam->tx->h);
|
||||||
|
// if (in_bounds(st->sam->tx, x, y, &st->bounds) == false) {
|
||||||
|
if (st->bounds.w - st->sam->tx->w < x) {
|
||||||
|
st->sam->tx = st->sam->idle;
|
||||||
|
x = st->bounds.w - st->sam->tx->w;
|
||||||
|
babble("*bonk*");
|
||||||
|
}
|
||||||
|
if (SDL_SetWindowPosition(st->win, x, y) == false)
|
||||||
|
shit("window pos set fail: %s", SDL_GetError());
|
||||||
|
|
||||||
|
render_it:
|
||||||
|
if (SDL_RenderTexture(st->ren, st->sam->tx->tex, NULL, NULL) == false) {
|
||||||
fuck("rendertexture fail: %s", SDL_GetError());
|
fuck("rendertexture fail: %s", SDL_GetError());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -37,7 +59,7 @@ put_samply_to_bed(Samply *sam)
|
||||||
}
|
}
|
||||||
|
|
||||||
Samply *
|
Samply *
|
||||||
wake_samply_up(SDL_Renderer *ren)
|
wake_samply_up(State *st)
|
||||||
{
|
{
|
||||||
Samply *sam;
|
Samply *sam;
|
||||||
|
|
||||||
|
@ -49,7 +71,7 @@ wake_samply_up(SDL_Renderer *ren)
|
||||||
}
|
}
|
||||||
|
|
||||||
babble("stand samply up!!");
|
babble("stand samply up!!");
|
||||||
sam->idle = load_image(ren, "samply/Samply.png");
|
sam->idle = load_image(st->ren, "samply/Samply.png");
|
||||||
if (!sam->idle) {
|
if (!sam->idle) {
|
||||||
fuck("samply would rather stay in bed. too bad!");
|
fuck("samply would rather stay in bed. too bad!");
|
||||||
put_samply_to_bed(sam);
|
put_samply_to_bed(sam);
|
||||||
|
@ -57,7 +79,7 @@ wake_samply_up(SDL_Renderer *ren)
|
||||||
}
|
}
|
||||||
|
|
||||||
babble("samply walk! part 1!!");
|
babble("samply walk! part 1!!");
|
||||||
sam->walka = load_image(ren, "samply/Samply_Walk1.png");
|
sam->walka = load_image(st->ren, "samply/Samply_Walk1.png");
|
||||||
if (!sam->walka) {
|
if (!sam->walka) {
|
||||||
fuck("samply would rather stay in bed. too bad!");
|
fuck("samply would rather stay in bed. too bad!");
|
||||||
put_samply_to_bed(sam);
|
put_samply_to_bed(sam);
|
||||||
|
@ -65,12 +87,14 @@ wake_samply_up(SDL_Renderer *ren)
|
||||||
}
|
}
|
||||||
|
|
||||||
babble("samply walk! part 2!!");
|
babble("samply walk! part 2!!");
|
||||||
sam->walkb = load_image(ren, "samply/Samply_Walk2.png");
|
sam->walkb = load_image(st->ren, "samply/Samply_Walk2.png");
|
||||||
if (!sam->walkb) {
|
if (!sam->walkb) {
|
||||||
fuck("samply would rather stay in bed. too bad!");
|
fuck("samply would rather stay in bed. too bad!");
|
||||||
put_samply_to_bed(sam);
|
put_samply_to_bed(sam);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sam->tx = sam->idle;
|
||||||
|
|
||||||
return sam;
|
return sam;
|
||||||
}
|
}
|
||||||
|
|
17
sdl.c
Normal file
17
sdl.c
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include "dat.h"
|
||||||
|
#include "fns.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
in_bounds(Image *i, int x, int y, SDL_Rect *bounds)
|
||||||
|
{
|
||||||
|
SDL_Rect r;
|
||||||
|
SDL_Rect irect = { x, y, i->w, i->h };
|
||||||
|
|
||||||
|
if (SDL_GetRectUnion(&irect, bounds, &r) == false) {
|
||||||
|
shit("rect union math fail: %s", SDL_GetError());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return SDL_RectsEqual(&irect, &r);
|
||||||
|
}
|
16
slutpet.c
16
slutpet.c
|
@ -95,8 +95,22 @@ SDL_AppInit(void **as, int argc, char **argv)
|
||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (SDL_EnableScreenSaver() == false)
|
||||||
|
shit("cannot disable screensaver: %s", SDL_GetError());
|
||||||
|
|
||||||
|
st->dpy = SDL_GetDisplayForWindow(st->win);
|
||||||
|
if (!st->dpy) {
|
||||||
|
fuck("cannot get current display: %s", SDL_GetError());
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SDL_GetDisplayBounds(st->dpy, &st->bounds) == false) {
|
||||||
|
fuck("cannot get display bounds: %s", SDL_GetError());
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
babble("create samply!!...");
|
babble("create samply!!...");
|
||||||
st->sam = wake_samply_up(st->ren);
|
st->sam = wake_samply_up(st);
|
||||||
if (!st->sam) {
|
if (!st->sam) {
|
||||||
fuck("samply said no. too bad!");
|
fuck("samply said no. too bad!");
|
||||||
return SDL_APP_FAILURE;
|
return SDL_APP_FAILURE;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue