Browse Source

Add sprites, get vectors setup and working. Compress graphics

Nathaniel van Diepen 2 years ago
parent
commit
d1cdd97185
8 changed files with 146 additions and 38 deletions
  1. 1 0
      src/gfx/convimg.yaml
  2. 10 3
      src/lib/object.c
  3. 3 2
      src/lib/object.h
  4. 22 0
      src/lib/sprite.c
  5. 16 0
      src/lib/sprite.h
  6. 26 3
      src/lib/vector.c
  7. 2 1
      src/lib/vector.h
  8. 66 29
      src/main.c

+ 1 - 0
src/gfx/convimg.yaml

@@ -10,6 +10,7 @@ palette: main_palette            : Palette and name of palette
 
 convert: main_images             : Convert images section
   palette: main_palette          : Use the above generated palette
+  compress: zx7
   images:                     : List of input images
     # - blackmage.png
     # Standing front

+ 10 - 3
src/object.c → src/lib/object.c

@@ -1,13 +1,21 @@
 #include "object.h"
+#include <debug.h>
+
+void Object_log(char* msg){
+    dbg_sprintf(dbgout, "[Object] %s\n", msg);
+}
 
 void Object_update(Object* self){}
 
 void Object_draw(Object* self){
     gfx_ScaledSprite_NoClip(
-        self->sprite, self->x, self->y,
+        self->sprite->data, self->x, self->y,
         self->scale.x, self->scale.y);
 }
-void Object_free(Object* self){
+
+void free_Object(Object* self){
+    Object_log("Freeing object");
+    free_Sprite(self->sprite);
     free(self);
 }
 
@@ -18,6 +26,5 @@ Object* new_Object(int x, int y){
     self->scale.x = self->scale.y = 1;
     self->update = &Object_update;
     self->draw = &Object_draw;
-    self->free = &Object_free;
     return self;
 }

+ 3 - 2
src/object.h → src/lib/object.h

@@ -2,6 +2,7 @@
 #define OBJECT_H_
 
 #include <graphx.h>
+#include "sprite.h"
 
 typedef struct Scale {
     int x;
@@ -12,13 +13,13 @@ typedef struct Object {
     int x;
     int y;
     Scale scale;
-    gfx_sprite_t* sprite;
+    Sprite* sprite;
 
     Object_func draw;
     Object_func update;
-    Object_func free;
 } Object;
 
+void free_Object(Object* self);
 Object* new_Object(int x, int y);
 
 #endif

+ 22 - 0
src/lib/sprite.c

@@ -0,0 +1,22 @@
+#include <compression.h>
+#include "sprite.h"
+#include <debug.h>
+
+void Sprite_log(char* msg){
+    dbg_sprintf(dbgout, "[Sprite] %s\n", msg);
+}
+
+void free_Sprite(Sprite* self){
+    Sprite_log("Freeing sprite");
+    free(self->data);
+    free(self);
+}
+
+Sprite* new_Sprite(int width, int height, void* compressed_data){
+	gfx_sprite_t* data = gfx_MallocSprite(width, height);
+    Sprite* self = (Sprite*)malloc(sizeof(Sprite));
+    zx7_Decompress(data, compressed_data);
+    self->data = data;
+
+    return self;
+}

+ 16 - 0
src/lib/sprite.h

@@ -0,0 +1,16 @@
+#ifndef SPRITE_H_
+#define SPRITE_H_
+
+#include <graphx.h>
+
+typedef void (*Sprite_func)(struct Sprite*);
+typedef struct Sprite {
+    int width;
+    int height;
+    gfx_sprite_t* data;
+} Sprite;
+
+void free_Sprite(Sprite* self);
+Sprite* new_Sprite(int width, int height, void* compressed_data);
+
+#endif

+ 26 - 3
src/lib/vector.c

@@ -1,8 +1,21 @@
 #include "vector.h"
+#include <debug.h>
+
+void Vector_log(char* msg){
+    dbg_sprintf(dbgout, "[Vector] %s\n", msg);
+}
+
+
+bool Vector_consume(struct Vector* self, const void* value){
+    bool success = vc_vector_push_back(self->_vector, value);
+    free(value);
+    return success;
+}
 
 bool Vector_push(struct Vector* self, const void* value){
     return vc_vector_push_back(self->_vector, value);
 }
+
 void* Vector_pop(struct Vector* self){
     void* item = vc_vector_end(self->_vector);
     if(vc_vector_pop_back(self->_vector)){
@@ -10,19 +23,27 @@ void* Vector_pop(struct Vector* self){
     }
     return item;
 }
+
 bool Vector_replace(struct Vector* self, size_t i, const void* value){
     return vc_vector_replace(self->_vector, i, value);
 }
+
 void Vector_clear(struct Vector* self){
     vc_vector_clear(self->_vector);
 }
+
 size_t Vector_count(struct Vector* self){
     return self->_vector->count;
 }
-void Vector_free(struct Vector* self){
-    vc_vector_release(self->_vector);
+
+void free_Vector(struct Vector* self){
+    Vector_log("Freeing vc_vector");
+    self->clear(self);
+    free(self->_vector);
+    Vector_log("Freeing Vector");
     free(self);
 }
+
 void* Vector_get(Vector* self, size_t i){
     return vc_vector_at(self->_vector, i);
 }
@@ -30,19 +51,21 @@ void* Vector_get(Vector* self, size_t i){
 Vector* new_Vector(int size, size_t obj_size, vc_vector_deleter* deleter){
     return new_VectorFromVC(vc_vector_create(size, obj_size, deleter));
 }
+
 Vector* new_VectorFromVC(vc_vector* vector){
     Vector* self = (Vector*)malloc(sizeof(Vector));
     self->_vector = vector;
 
+    self->consume = &Vector_consume;
     self->push = &Vector_push;
     self->pop = &Vector_pop;
     self->replace = &Vector_replace;
     self->clear = &Vector_clear;
     self->count = &Vector_count;
-    self->free = &Vector_free;
     self->get = &Vector_get;
     return self;
 }
+
 Vector* copy_Vector(struct Vector* vector){
     return new_VectorFromVC(vc_vector_create_copy(vector->_vector));
 }

+ 2 - 1
src/lib/vector.h

@@ -13,15 +13,16 @@ typedef void* (*Vector_func_get)(struct Vector*, size_t i);
 typedef struct Vector {
     vc_vector* _vector;
 
+    Vector_func_push consume;
     Vector_func_push push;
     Vector_func_pop pop;
     Vector_func_replace replace;
     Vector_func clear;
     Vector_func_count count;
-    Vector_func free;
     Vector_func_get get;
 } Vector;
 
+void free_Vector(struct Vector* self);
 Vector* new_Vector(int size, size_t obj_size, vc_vector_deleter* deleter);
 Vector* new_VectorFromVC(vc_vector* vector);
 #endif

+ 66 - 29
src/main.c

@@ -2,39 +2,67 @@
 #include <graphx.h>
 #include <keypadc.h>
 #include <debug.h>
+#include <compression.h>
 
 #include "gfx/main_gfx.h"
 #include "lib/vector.h"
-#include "object.h"
+#include "lib/sprite.h"
+#include "lib/object.h"
 
 float scale = 3.0;
 int frameskip = 10;
 
-int max_width = LCD_WIDTH;
-int max_height = LCD_HEIGHT;
-kb_key_t arrows;
-int frame = 0;
-Object* player;
 
 void log(char* msg){
     dbg_sprintf(dbgout, "[TBP] %s\n", msg);
 }
 
 int main(void){
-    log("New Object");
-    player = new_Object(0, 0);
-    log("Get max sizes");
+    kb_key_t arrows;
+    int max_width = LCD_WIDTH;
+    int max_height = LCD_HEIGHT;
+    int frame = 0;
+    size_t i;
+    Object* object;
+    Object* player = new_Object(0, 0);
+    Vector* objects = new_Vector(1, sizeof(Object), &free_Object);
+    Vector* sprites = new_Vector(22, sizeof(Sprite), &free_Sprite);
+    sprites->consume(sprites, new_Sprite(bm1_width, bm1_height, bm1_compressed));
+    sprites->consume(sprites, new_Sprite(bm2_width, bm2_height, bm2_compressed));
+    sprites->consume(sprites, new_Sprite(bm3_width, bm3_height, bm3_compressed));
+    sprites->consume(sprites, new_Sprite(bm4_width, bm4_height, bm4_compressed));
+    sprites->consume(sprites, new_Sprite(bm5_width, bm5_height, bm5_compressed));
+    sprites->consume(sprites, new_Sprite(bm6_width, bm6_height, bm6_compressed));
+    sprites->consume(sprites, new_Sprite(bm7_width, bm7_height, bm7_compressed));
+    sprites->consume(sprites, new_Sprite(bm8_width, bm8_height, bm8_compressed));
+    sprites->consume(sprites, new_Sprite(bm9_width, bm9_height, bm9_compressed));
+    sprites->consume(sprites, new_Sprite(bm10_width, bm10_height, bm10_compressed));
+    sprites->consume(sprites, new_Sprite(bm11_width, bm11_height, bm11_compressed));
+    sprites->consume(sprites, new_Sprite(bm12_width, bm12_height, bm12_compressed));
+    sprites->consume(sprites, new_Sprite(bm13_width, bm13_height, bm13_compressed));
+    sprites->consume(sprites, new_Sprite(bm14_width, bm14_height, bm14_compressed));
+    sprites->consume(sprites, new_Sprite(bm15_width, bm15_height, bm15_compressed));
+    sprites->consume(sprites, new_Sprite(bm16_width, bm16_height, bm16_compressed));
+    sprites->consume(sprites, new_Sprite(bm17_width, bm17_height, bm17_compressed));
+    sprites->consume(sprites, new_Sprite(bm18_width, bm18_height, bm18_compressed));
+    sprites->consume(sprites, new_Sprite(bm19_width, bm19_height, bm19_compressed));
+    sprites->consume(sprites, new_Sprite(bm20_width, bm20_height, bm20_compressed));
+    sprites->consume(sprites, new_Sprite(bm21_width, bm21_height, bm21_compressed));
+    sprites->consume(sprites, new_Sprite(bm22_width, bm22_height, bm22_compressed));
 
-    if(bm3->height > bm5->height){
-        max_height -= bm3->height * scale;
-    }else{
-        max_height -= bm5->height * scale;
-    }
-    if(bm11->width > bm13->width){
-        max_width -= bm11->width * scale;
-    }else{
-        max_width -= bm13->width * scale;
+    if(!objects->push(objects, player)){
+        log("Unable to add player to stack");
+        free_Vector(objects);
+        free_Object(player);
+        return 1;
     }
+    free_Object(player);
+    player = objects->get(objects, 0);
+    player->sprite = sprites->get(sprites, 0);
+    log("Get max sizes");
+
+    max_width -= player->sprite->data->width * scale;
+    max_height -= player->sprite->data->height * scale;
     log("Set scale");
 
     player->scale.x = scale;
@@ -48,7 +76,7 @@ int main(void){
 
     log("Main loop");
     do{
-        player->sprite = bm1;
+        player->sprite = sprites->get(sprites, 0);
         kb_Scan();
         arrows = kb_Data[7];
 
@@ -56,18 +84,18 @@ int main(void){
             if(player->y < max_height){
                 player->y++;
                 if(frame > 2){
-                    player->sprite = bm3;
+                    player->sprite = sprites->get(sprites, 2);
                 }else{
-                    player->sprite = bm5;
+                    player->sprite = sprites->get(sprites, 4);
                 }
             }
         }else if(arrows & kb_Up){
             if(player->y > 0){
                 player->y--;
                 if(frame > 2){
-                    player->sprite = bm19;
+                    player->sprite = sprites->get(sprites, 18);
                 }else{
-                    player->sprite = bm21;
+                    player->sprite = sprites->get(sprites, 20);
                 }
             }
         }
@@ -75,30 +103,39 @@ int main(void){
             if(player->x < max_width){
                 player->x++;
                 if(frame > 2){
-                    player->sprite = bm11;
+                    player->sprite = sprites->get(sprites, 10);
                 }else{
-                    player->sprite = bm13;
+                    player->sprite = sprites->get(sprites, 12);
                 }
             }
         }else if(arrows & kb_Left){
             if(player->x > 0){
                 player->x--;
                 if(frame > 2){
-                    player->sprite = bm7;
+                    player->sprite = sprites->get(sprites, 6);
                 }else{
-                    player->sprite = bm9;
+                    player->sprite = sprites->get(sprites, 8);
                 }
             }
         }
         gfx_FillScreen(0);
-        player->draw(player);
+        for(i = 0; i < objects->count(objects); i++){
+            object = objects->get(objects, i);
+            object->draw(object);
+        }
         gfx_BlitBuffer();
         frame++;
         if(frame > frameskip){
             frame = 0;
         }
     }while (! (kb_Data[6] & kb_Clear));
-    player->free(player);
+    log("Freeing sprites stack");
+    free_Vector(sprites);
+    player->sprite = NULL;
+    log("Freeing objects stack");
+    free_Vector(objects);
+    log("End graphics");
     gfx_End();
+    log("Done");
     return 0;
 }