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.

97 lines
2.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include "utils.h"
  4. #pragma comment(lib,"SOIL.lib")
  5. char* LoadFileContent(const char*path)
  6. {
  7. char *pFileContent = NULL;
  8. FILE*pFile = fopen(path, "rb");
  9. if (pFile)
  10. {
  11. fseek(pFile, 0, SEEK_END);
  12. int nLen = ftell(pFile);
  13. if (nLen > 0)
  14. {
  15. rewind(pFile);
  16. pFileContent = new char[nLen + 1];
  17. fread(pFileContent, 1, nLen, pFile);
  18. pFileContent[nLen] = '\0';
  19. }
  20. fclose(pFile);
  21. }
  22. return pFileContent;
  23. }
  24. unsigned char* LoadBMP(const char*path, int &width, int &height)
  25. {
  26. unsigned char*imageData = nullptr;
  27. FILE *pFile = fopen(path, "rb");
  28. if (pFile)
  29. {
  30. BITMAPFILEHEADER bfh;
  31. fread(&bfh, sizeof(BITMAPFILEHEADER), 1, pFile);
  32. if (bfh.bfType == 0x4D42)
  33. {
  34. BITMAPINFOHEADER bih;
  35. fread(&bih, sizeof(BITMAPINFOHEADER), 1, pFile);
  36. width = bih.biWidth;
  37. height = bih.biHeight;
  38. int pixelCount = width*height * 3;
  39. imageData = new unsigned char[pixelCount];
  40. fseek(pFile, bfh.bfOffBits, SEEK_SET);
  41. fread(imageData, 1, pixelCount, pFile);
  42. unsigned char temp;
  43. for (int i = 0; i < pixelCount; i += 3)
  44. {
  45. temp = imageData[i + 2];
  46. imageData[i + 2] = imageData[i];
  47. imageData[i] = temp;
  48. }
  49. }
  50. fclose(pFile);
  51. }
  52. return imageData;
  53. }
  54. GLuint CreateTextureFromFile(const char*filePath)
  55. {
  56. GLuint texture = SOIL_load_OGL_texture(filePath, 0, 0, SOIL_FLAG_POWER_OF_TWO);
  57. return texture;
  58. }
  59. void CheckGLError(const char*file, int line)
  60. {
  61. GLenum error = glGetError();
  62. if (error != GL_NO_ERROR)
  63. {
  64. switch (error)
  65. {
  66. case GL_INVALID_ENUM:
  67. printf("GL Error GL_INVALID_ENUM %s : %d\n", file, line);
  68. break;
  69. case GL_INVALID_VALUE:
  70. printf("GL Error GL_INVALID_VALUE %s : %d\n", file, line);
  71. break;
  72. case GL_INVALID_OPERATION:
  73. printf("GL Error GL_INVALID_OPERATION %s : %d\n", file, line);
  74. break;
  75. case GL_STACK_OVERFLOW:
  76. printf("GL Error GL_STACK_OVERFLOW %s : %d\n", file, line);
  77. break;
  78. case GL_STACK_UNDERFLOW:
  79. printf("GL Error GL_STACK_UNDERFLOW %s : %d\n", file, line);
  80. break;
  81. case GL_OUT_OF_MEMORY:
  82. printf("GL Error GL_OUT_OF_MEMORY %s : %d\n", file, line);
  83. break;
  84. default:
  85. printf("GL Error 0x%x %s : %d\n", error, file, line);
  86. break;
  87. }
  88. }
  89. }