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.

tileset.cpp 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: tileset.cpp
  5. *
  6. * Description:
  7. *
  8. * Version: 1.0
  9. * Created: 04/02/2014 11:21:50 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "tileset.h"
  19. #include "bitmap.h"
  20. #include <SDL2/SDL_opengl.h>
  21. #include <iostream>
  22. namespace vtk {
  23. Tileset::Tileset() {
  24. tilesize = 32;
  25. maxTextures = 10;
  26. textureIndex = 0;
  27. }
  28. void Tileset::bind() {
  29. glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
  30. }
  31. bool Tileset::addTexture(const std::string& fileName, const std::string& textureName) {
  32. textureFiles[fileName] = textureName;
  33. return true;
  34. }
  35. void Tileset::updateTextureAt(const int& index, const std::string& fileName) {
  36. Bitmap bitmap;
  37. bitmap.loadFromFile(fileName);
  38. glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, index, tilesize, tilesize, 1, GL_RGBA,
  39. GL_UNSIGNED_BYTE, bitmap.getPixelDataPtr());
  40. }
  41. int Tileset::getTextureIndex(const std::string& textureName) {
  42. if (textureIndexes.find(textureName) == textureIndexes.end()) {
  43. textureIndexes[textureName] = textureIndex;
  44. textureIndex++;
  45. }
  46. return textureIndexes[textureName];
  47. }
  48. void Tileset::buildTexture() {
  49. textureIndexes.clear();
  50. textureIndex = 0;
  51. glGenTextures(1, &texture);
  52. glActiveTexture(GL_TEXTURE0);
  53. glBindTexture(GL_TEXTURE_2D_ARRAY, texture);
  54. glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA,
  55. tilesize,
  56. tilesize,
  57. maxTextures, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  58. NULL);
  59. glTexParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  60. glTexParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  61. glTexParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  62. glTexParameteri (GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  63. }
  64. void Tileset::deleteTexture() {
  65. }
  66. }