bullets
This commit is contained in:
54
src/sprite.c
54
src/sprite.c
@@ -1,33 +1,21 @@
|
||||
#include "sprite.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
void* xmalloc(size_t size)
|
||||
Sprite* sprite_from_texture(Texture2D tex,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color,
|
||||
Arena* arena)
|
||||
{
|
||||
void* p = malloc(size);
|
||||
if (!p) abort();
|
||||
return p;
|
||||
}
|
||||
|
||||
Sprite* sprite_new(const char* path,
|
||||
Vector2* pos,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color)
|
||||
{
|
||||
assert(path && "path must not be empty");
|
||||
|
||||
Sprite* sprite = xmalloc(sizeof(Sprite));
|
||||
Sprite* sprite = arena_alloc(arena, sizeof(Sprite), sizeof(Sprite));
|
||||
if (!sprite) abort();
|
||||
|
||||
sprite->tex = LoadTexture(path);
|
||||
|
||||
sprite->pos = pos != NULL ? *pos : (Vector2){0, 0};
|
||||
sprite->tex = tex;
|
||||
|
||||
sprite->source =
|
||||
source != NULL
|
||||
@@ -45,7 +33,20 @@ Sprite* sprite_new(const char* path,
|
||||
return sprite;
|
||||
}
|
||||
|
||||
void sprite_draw(Sprite* s)
|
||||
Sprite* sprite_new(const char* path,
|
||||
Vector2* anchor,
|
||||
Rectangle* source,
|
||||
Color* color,
|
||||
Arena* arena,
|
||||
TextureManager* tm)
|
||||
{
|
||||
assert(path && "path must not be empty");
|
||||
|
||||
Texture2D tex = tm_load_texture(tm, path);
|
||||
return sprite_from_texture(tex, anchor, source, color, arena);
|
||||
}
|
||||
|
||||
void sprite_draw(Sprite* s, Vector2 pos)
|
||||
{
|
||||
Rectangle source = s->source;
|
||||
|
||||
@@ -54,23 +55,14 @@ void sprite_draw(Sprite* s)
|
||||
source.x = source.width * (float)s->animation->current_frame;
|
||||
}
|
||||
|
||||
DrawTextureRec(
|
||||
s->tex, source, Vector2Subtract(s->pos, s->anchor), s->color);
|
||||
}
|
||||
|
||||
void sprite_free(Sprite* s)
|
||||
{
|
||||
if (s->animation) free(s->animation);
|
||||
UnloadTexture(s->tex);
|
||||
free(s);
|
||||
DrawTextureRec(s->tex, source, Vector2Subtract(pos, s->anchor), s->color);
|
||||
}
|
||||
|
||||
//
|
||||
// Animation System
|
||||
//
|
||||
void animation_update(Animation* a)
|
||||
void animation_update(Animation* a, float dt)
|
||||
{
|
||||
float dt = GetFrameTime();
|
||||
a->current_time += dt;
|
||||
|
||||
if (a->current_time >= 1.0f / a->fps) {
|
||||
|
||||
Reference in New Issue
Block a user