graphics.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. */
  3. #pragma once
  4. #include <stdint.h>
  5. #include <stddef.h>
  6. #define GFX_W(ctx) ((ctx)->width) /* Display width */
  7. #define GFX_H(ctx) ((ctx)->height) /* Display height */
  8. #define GFX_B(ctx) ((ctx)->depth / 8) /* Display byte depth */
  9. #define _RED(color) ((color & 0x00FF0000) / 0x10000)
  10. #define _GRE(color) ((color & 0x0000FF00) / 0x100)
  11. #define _BLU(color) ((color & 0x000000FF) / 0x1)
  12. #define _ALP(color) ((color & 0xFF000000) / 0x1000000)
  13. /*
  14. * Macros make verything easier.
  15. */
  16. #define GFX(ctx,x,y) *((uint32_t *)&((ctx)->backbuffer)[(GFX_W(ctx) * (y) + (x)) * GFX_B(ctx)])
  17. #define GFXR(ctx,x,y) *((uint32_t *)&((ctx)->buffer)[(GFX_W(ctx) * (y) + (x)) * GFX_B(ctx)])
  18. #define SPRITE(sprite,x,y) sprite->bitmap[sprite->width * (y) + (x)]
  19. #define SMASKS(sprite,x,y) sprite->masks[sprite->width * (y) + (x)]
  20. typedef struct sprite {
  21. uint16_t width;
  22. uint16_t height;
  23. uint32_t * bitmap;
  24. uint32_t * masks;
  25. uint32_t blank;
  26. uint8_t alpha;
  27. } sprite_t;
  28. typedef struct context {
  29. uint16_t width;
  30. uint16_t height;
  31. uint16_t depth;
  32. uint32_t size;
  33. char * buffer;
  34. char * backbuffer;
  35. void * clips;
  36. } gfx_context_t;
  37. extern gfx_context_t * init_graphics_fullscreen();
  38. extern gfx_context_t * init_graphics_fullscreen_double_buffer();
  39. extern void reinit_graphics_fullscreen(gfx_context_t * ctx);
  40. #define ALPHA_OPAQUE 0
  41. #define ALPHA_MASK 1
  42. #define ALPHA_EMBEDDED 2
  43. #define ALPHA_INDEXED 3
  44. extern uint32_t rgb(uint8_t r, uint8_t g, uint8_t b);
  45. extern uint32_t rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
  46. extern uint32_t alpha_blend(uint32_t bottom, uint32_t top, uint32_t mask);
  47. extern uint32_t alpha_blend_rgba(uint32_t bottom, uint32_t top);
  48. extern uint32_t framebuffer_stride(void);
  49. extern void flip(gfx_context_t * ctx);
  50. void clear_buffer(gfx_context_t * ctx);
  51. extern gfx_context_t * init_graphics_sprite(sprite_t * sprite);
  52. extern sprite_t * create_sprite(size_t width, size_t height, int alpha);
  53. extern void blur_context(gfx_context_t * _dst, gfx_context_t * _src, double amount);
  54. extern void blur_context_no_vignette(gfx_context_t * _dst, gfx_context_t * _src, double amount);
  55. extern void blur_context_box(gfx_context_t * _src, int radius);
  56. extern void sprite_free(sprite_t * sprite);
  57. extern void load_sprite(sprite_t * sprite, char * filename);
  58. //extern int load_sprite_png(sprite_t * sprite, char * file);
  59. extern void draw_sprite(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y);
  60. extern void draw_line(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color);
  61. extern void draw_line_thick(gfx_context_t * ctx, int32_t x0, int32_t x1, int32_t y0, int32_t y1, uint32_t color, char thickness);
  62. extern void draw_fill(gfx_context_t * ctx, uint32_t color);
  63. extern void draw_sprite_scaled(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height);
  64. extern void draw_sprite_scaled_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, uint16_t width, uint16_t height, float alpha);
  65. extern void draw_sprite_alpha(gfx_context_t * ctx, sprite_t * sprite, int32_t x, int32_t y, float alpha);
  66. //extern void context_to_png(FILE * file, gfx_context_t * ctx);
  67. extern uint32_t premultiply(uint32_t color);
  68. extern void gfx_add_clip(gfx_context_t * ctx, int32_t x, int32_t y, int32_t w, int32_t h);
  69. extern void gfx_clear_clip(gfx_context_t * ctx);