You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

4 years ago
  1. #include "util.h"
  2. #include<functional>
  3. static RenderableBuffer* sRenderableBuffer = nullptr;
  4. void InitRenderableBuffer(HDC dc, int width, int height)
  5. {
  6. sRenderableBuffer = new RenderableBuffer();
  7. sRenderableBuffer->mDC = CreateCompatibleDC(dc);
  8. BITMAPINFO bitmapinfo = {};
  9. bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  10. bitmapinfo.bmiHeader.biPlanes = 1;
  11. bitmapinfo.bmiHeader.biBitCount = 32;
  12. bitmapinfo.bmiHeader.biCompression = BI_RGB;
  13. bitmapinfo.bmiHeader.biWidth = width;
  14. bitmapinfo.bmiHeader.biHeight = height;
  15. sRenderableBuffer->mBitmap = CreateDIBSection(sRenderableBuffer->mDC, &bitmapinfo, DIB_RGB_COLORS, (void**)&sRenderableBuffer->mColorBuffer, 0, 0);
  16. SelectObject(sRenderableBuffer->mDC, sRenderableBuffer->mBitmap);
  17. sRenderableBuffer->mHeight = height;
  18. sRenderableBuffer->mWidth = width;
  19. sRenderableBuffer->mPixelCount = height * width;
  20. }
  21. void SetColor(int x, int y, AByte r, AByte g, AByte b, AByte a)
  22. {
  23. AUint color = (a << 24) + (r << 16) + (g << 8) + b;
  24. int pixel_index = y * sRenderableBuffer->mWidth + x;
  25. if (pixel_index < sRenderableBuffer->mPixelCount) {
  26. sRenderableBuffer->mColorBuffer[pixel_index] = color;
  27. }
  28. }
  29. void ASwapBuffers(HDC dc)
  30. {
  31. BitBlt(dc, 0, 0, sRenderableBuffer->mWidth, sRenderableBuffer->mHeight, sRenderableBuffer->mDC, 0,0, SRCCOPY);
  32. }
  33. float randf()
  34. {
  35. return float(rand()) / float(RAND_MAX);
  36. }
  37. float srandf()
  38. {
  39. return randf() * 2.0f - 1.0f;
  40. }
  41. unsigned char* LoadFileContent(const char* path, int& file_size) {
  42. FILE* file = fopen(path, "rb");
  43. if (file != nullptr) {
  44. fseek(file, 0, SEEK_END);
  45. file_size = ftell(file);
  46. if (file_size > 0) {
  47. rewind(file);
  48. unsigned char* content = new unsigned char[file_size + 1];
  49. fread(content, 1, file_size, file);
  50. content[file_size] = 0;
  51. fclose(file);
  52. return content;
  53. }
  54. fclose(file);
  55. }
  56. return nullptr;
  57. }
  58. unsigned char* DecodeBMP(unsigned char* bmp_file_content, int& width, int& height) {
  59. if (0x4D42 == *((unsigned short*)bmp_file_content)) {
  60. int pixel_data_offset = *((int*)(bmp_file_content + 10));
  61. width = *((int*)(bmp_file_content + 18));
  62. height = *((int*)(bmp_file_content + 22));
  63. unsigned char * pixel_data = bmp_file_content + pixel_data_offset;
  64. //bgr->rgb
  65. for (int i = 0; i < width * height; i++) {
  66. int pixel_data_start_index = i * 3;
  67. unsigned char r = pixel_data[pixel_data_start_index + 2];
  68. pixel_data[pixel_data_start_index + 2] = pixel_data[pixel_data_start_index];
  69. pixel_data[pixel_data_start_index] = r;
  70. }
  71. return pixel_data;
  72. }
  73. return nullptr;
  74. }