display.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * 3D Engine
  3. * display.h:
  4. * Based on pikuma.com 3D software renderer in C
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 01/03/2021.
  8. */
  9. #ifndef THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
  10. #define THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H
  11. #include <stdbool.h>
  12. #include <SDL.h>
  13. /***********************************************************************************************************************
  14. * Data types
  15. **********************************************************************************************************************/
  16. typedef uint32_t colour_t;
  17. /***********************************************************************************************************************
  18. * Constants
  19. **********************************************************************************************************************/
  20. #define FPS (60)
  21. #define FRAME_TARGET_TIME (1000 / FPS)
  22. /***********************************************************************************************************************
  23. * Global variables
  24. **********************************************************************************************************************/
  25. extern SDL_Window *window;
  26. extern SDL_Renderer *renderer;
  27. extern colour_t *frameBuffer;
  28. extern double *zBuffer;
  29. extern SDL_Texture *frameBufferTexture;
  30. extern int32_t windowWidth;
  31. extern int32_t windowHeight;
  32. extern bool doZBuffer;
  33. /***********************************************************************************************************************
  34. * Prototypes
  35. **********************************************************************************************************************/
  36. /* --- Window functions --- */
  37. bool initialiseWindow(bool fullScreen);
  38. void destroyWindow();
  39. void renderFrameBuffer();
  40. /* --- Drawing functions --- */
  41. void clearZBuffer();
  42. void clearFrameBuffer(colour_t colour);
  43. void drawPixel(int32_t x, int32_t y, colour_t colour);
  44. void drawGrid(int spacing, colour_t colour);
  45. void drawRectangle(int32_t x, int32_t y, int32_t w, int32_t h, colour_t colour);
  46. void drawVLine(int32_t x, int32_t y0, int32_t y1, colour_t colour);
  47. void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour);
  48. void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour);
  49. void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...);
  50. static inline void intSwap(int *a, int *b)
  51. {
  52. int tmp = *a;
  53. *a = *b;
  54. *b = tmp;
  55. }
  56. static inline void doubleSwap(double *a, double *b)
  57. {
  58. double tmp = *a;
  59. *a = *b;
  60. *b = tmp;
  61. }
  62. #define MAKE_RGB(_r, _g, _b) ((0xFF000000) | ((_b & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
  63. #define MAKE_ARGB(_a, _r, _g, _b) (((_a & 0xFF) << 24) | ((_g & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
  64. #endif /* THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H */