summaryrefslogtreecommitdiff
path: root/trunk/example.c
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/example.c')
-rw-r--r--trunk/example.c173
1 files changed, 89 insertions, 84 deletions
diff --git a/trunk/example.c b/trunk/example.c
index 5de3139..25fff0a 100644
--- a/trunk/example.c
+++ b/trunk/example.c
@@ -4,93 +4,16 @@
#include "librosprite.h"
-/* color is 0xrrggbbaa */
-void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color)
-{
- uint32_t* pixel = ((uint32_t*) (surface->pixels)) + (y * surface->pitch/4) + x;
- /* pretty sure SDL can do this, but can't figure out how */
- uint32_t bg_color = ((int) (x / 4.0) + ((int)(y / 4.0) % 2)) % 2 ? 0x99 : 0x66;
-
- uint32_t alpha = color & 0x000000ff;
- uint32_t r = (color & 0xff000000) >> 24;
- uint32_t g = (color & 0x00ff0000) >> 16;
- uint32_t b = (color & 0x0000ff00) >> 8;
- r = ((alpha / 255.0) * r) + (((255-alpha) / 255.0) * bg_color);
- g = ((alpha / 255.0) * g) + (((255-alpha) / 255.0) * bg_color);
- b = ((alpha / 255.0) * b) + (((255-alpha) / 255.0) * bg_color);
- uint32_t mapped_color = SDL_MapRGB(surface->format, r, g, b);
-
- *pixel = mapped_color;
-}
-
-void sdl_blank(SDL_Surface* surface)
-{
- for (uint32_t y = 0; y < (uint32_t) surface->h; y++) {
- for (uint32_t x = 0; x < (uint32_t) surface->w; x++) {
- sdl_draw_pixel(surface, x, y, (uint32_t) ((int) (x / 4.0) + ((int)(y / 4.0) % 2)) % 2 ? 0x999999ff : 0x666666ff);
- }
- }
-}
-
-int load_file_to_memory(const char *filename, uint8_t **result)
-{
- int size = 0;
- FILE *f = fopen(filename, "rb");
- if (f == NULL)
- {
- *result = NULL;
- return -1; // -1 means file opening fail
- }
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fseek(f, 0, SEEK_SET);
- *result = (uint8_t *)malloc(size+1);
- if ((unsigned int) size != fread(*result, sizeof(char), size, f))
- {
- free(*result);
- return -2; // -2 means file reading fail
- }
- fclose(f);
- (*result)[size] = 0;
- return size;
-}
-
-int create_file_context(char* filename, void** result)
-{
- FILE *f = fopen(filename, "rb");
- if (!f) {
- *result = NULL;
- return -1;
- }
-
- struct rosprite_file_context* ctx;
- if (rosprite_create_file_context(f, &ctx) != ROSPRITE_OK) {
- return -1;
- }
- *result = ctx;
-
- return 0;
-}
-
-int create_mem_context(char* filename, void** result)
-{
- uint8_t* content;
-
- int size = load_file_to_memory(filename, &content);
- if (size < 0) return -1;
- struct rosprite_mem_context* ctx;
- if (rosprite_create_mem_context(content, size, &ctx) != ROSPRITE_OK) {
- return -1;
- }
- *result = ctx;
-
- return 0;
-}
+void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color);
+void sdl_blank(SDL_Surface* surface);
+int load_file_to_memory(const char *filename, uint8_t **result);
+int create_file_context(char* filename, void** result);
+int create_mem_context(char* filename, void** result);
int main(int argc, char *argv[])
{
if (argc < 2) {
- printf("Usage: example spritefile\n");
+ fprintf(stderr, "Usage: example spritefile\n");
exit(EXIT_FAILURE);
}
@@ -106,7 +29,8 @@ int main(int argc, char *argv[])
printf("Loading %s\n", filename);
struct rosprite_area* sprite_area;
- if (!rosprite_load(rosprite_file_reader, ctx, &sprite_area)) {
+ if (rosprite_load(rosprite_file_reader, ctx, &sprite_area) != ROSPRITE_OK) {
+ fprintf(stderr, "Error loading spritefile\n");
exit(EXIT_FAILURE);
};
printf("sprite_count %u\n", sprite_area->sprite_count);
@@ -151,4 +75,85 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS;
}
+int create_file_context(char* filename, void** result)
+{
+ FILE *f = fopen(filename, "rb");
+ if (!f) {
+ *result = NULL;
+ return -1;
+ }
+ struct rosprite_file_context* ctx;
+ if (rosprite_create_file_context(f, &ctx) != ROSPRITE_OK) {
+ return -1;
+ }
+ *result = ctx;
+
+ return 0;
+}
+
+int create_mem_context(char* filename, void** result)
+{
+ uint8_t* content;
+
+ int size = load_file_to_memory(filename, &content);
+ if (size < 0) return -1;
+ struct rosprite_mem_context* ctx;
+ if (rosprite_create_mem_context(content, size, &ctx) != ROSPRITE_OK) {
+ return -1;
+ }
+ *result = ctx;
+
+ return 0;
+}
+
+int load_file_to_memory(const char *filename, uint8_t **result)
+{
+ int size = 0;
+ FILE *f = fopen(filename, "rb");
+ if (f == NULL)
+ {
+ *result = NULL;
+ return -1; // -1 means file opening fail
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ *result = (uint8_t *)malloc(size+1);
+ if ((unsigned int) size != fread(*result, sizeof(char), size, f))
+ {
+ free(*result);
+ return -2; // -2 means file reading fail
+ }
+ fclose(f);
+ (*result)[size] = 0;
+ return size;
+}
+
+/* color is 0xrrggbbaa */
+void sdl_draw_pixel(SDL_Surface* surface, uint32_t x, uint32_t y, uint32_t color)
+{
+ uint32_t* pixel = ((uint32_t*) (surface->pixels)) + (y * surface->pitch/4) + x;
+ /* pretty sure SDL can do this, but can't figure out how */
+ uint32_t bg_color = ((int) (x / 4.0) + ((int)(y / 4.0) % 2)) % 2 ? 0x99 : 0x66;
+
+ uint32_t alpha = color & 0x000000ff;
+ uint32_t r = (color & 0xff000000) >> 24;
+ uint32_t g = (color & 0x00ff0000) >> 16;
+ uint32_t b = (color & 0x0000ff00) >> 8;
+ r = ((alpha / 255.0) * r) + (((255-alpha) / 255.0) * bg_color);
+ g = ((alpha / 255.0) * g) + (((255-alpha) / 255.0) * bg_color);
+ b = ((alpha / 255.0) * b) + (((255-alpha) / 255.0) * bg_color);
+ uint32_t mapped_color = SDL_MapRGB(surface->format, r, g, b);
+
+ *pixel = mapped_color;
+}
+
+void sdl_blank(SDL_Surface* surface)
+{
+ for (uint32_t y = 0; y < (uint32_t) surface->h; y++) {
+ for (uint32_t x = 0; x < (uint32_t) surface->w; x++) {
+ sdl_draw_pixel(surface, x, y, (uint32_t) ((int) (x / 4.0) + ((int)(y / 4.0) % 2)) % 2 ? 0x999999ff : 0x666666ff);
+ }
+ }
+}