Skip to content

Commit

Permalink
sprite_read; sprite_draw; sprite_deloc. TODO make sprite array reader
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroManse committed Nov 26, 2023
1 parent 7165d1f commit 894e4d0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
14 changes: 14 additions & 0 deletions draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,19 @@ void fill_circle(
location[(y+_y)*frame.skip+x+_x] = write;
}

void sprite_draw(
const struct frame frame,
const int y, const int x,
const struct sprite *sprite
) {
#ifndef RELEASE
assert(in_frame(frame, y, x));
assert(in_frame(frame, y+sprite->heigth, x+sprite->width));
#endif

color *location = frame_pos(frame, y, x);
for (uint32_t _y = 0; _y < sprite->heigth; _y++)
for (uint32_t _x = 0; _x < sprite->width; _x++)
location[_y*frame.skip+_x] = sprite->content[_y*(sprite->width)+_x];
}

5 changes: 5 additions & 0 deletions fba.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ typedef struct {
typedef uint32_t color;
#define COL(R, G, B) (color)( (R<<16) + (G<<8) + B )

struct sprite {
uint32_t width;
uint32_t heigth;
color *content;
};

struct frame {
color* fbmem;
Expand Down
7 changes: 4 additions & 3 deletions maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ long double lerp(
return a * (1-p) + (b*p);
}

color* frame_pos ( struct frame jar, int y, int x ) {
return jar.fbmem + (y+jar.yoff)*jar.skip + (jar.xoff+x);
}
#define frame_pos(frame, y, x) (frame.fbmem + (y+frame.yoff)*frame.skip + (frame.xoff+x))
//color* frame_pos ( struct frame jar, int y, int x ) {
// return jar.fbmem + (y+jar.yoff)*jar.skip + (jar.xoff+x);
//}

bool in_frame(struct frame jar, int y, int x) {
return ( (unsigned)y < jar.rows && (unsigned)x < jar.cols );
Expand Down
46 changes: 39 additions & 7 deletions test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "fba.h"
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#ifndef __useconds_t
#define __useconds_t unsigned int
Expand All @@ -14,16 +15,47 @@ int fsleep (long double t) {
return usleep((int) lroundl(1000000.0 * t));
}

void sprite_deloc(struct sprite *sprite) {
free(sprite->content);
free(sprite);
}

struct sprite *sprite_read(char *filename) {
struct sprite *sprite = malloc(sizeof(*sprite));
int fd = open(filename, O_RDONLY);
assert(read(fd,
(uint32_t*)sprite,
sizeof(uint32_t)*2)
);
color *content_buffer = calloc(
sprite->width * sprite->heigth,
sizeof(color)
);
assert(read(fd, content_buffer,
sizeof(color) * sprite->width * sprite->heigth
));
sprite->content = content_buffer;
close(fd);
return sprite;
}

int main() {
struct frame frame = InitFb();

for (int r = 1; r<100; r++) {
fill_circle(frame,
100, 100, r,
0xa0a0a0
);
fsleep(.3);
}
struct sprite *sprite = sprite_read("a.font");

sprite_draw(frame, 100, 100, sprite);

sprite_deloc(sprite);

// growing ball
// for (int r = 1; r<100; r++) {
// fill_circle(frame,
// 100, 100, r,
// 0xa0a0a0
// );
// fsleep(.3);
// }

// draw_free_line(
// frame, 100, 100,
Expand Down

0 comments on commit 894e4d0

Please sign in to comment.