-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinygl.h
1191 lines (1098 loc) · 39.5 KB
/
tinygl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <SDL.h>
#include <glew.h>
#include <SDL_opengl.h>
#include <GL\glu.h>
#include <SDL_timer.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
// #include <ctime>
// #include <algorithm>
extern SDL_Window* window;
TTF_Font* font;
void logSDLError(std::ostream &os, const std::string &msg) {
os << msg << " SDL Error: " << SDL_GetError() << std::endl;
}
void logGLError(std::ostream &os, const std::string &msg, GLenum error) {
os << msg << " OpenGL Error: " << gluErrorString(error) << std::endl;
}
SDL_Color BLACK = { 0, 0, 0,255};
SDL_Color WHITE = {255,255,255,255};
SDL_Color GREEN = { 0,255, 0,255};
SDL_Color RED = {255, 0, 0,255};
/* VECTORS */
template <class T> struct v2 {
union {
struct {T u, v;};
struct {T x, y;};
struct {T s, t;};
T raw[2];
};
v2() : u(0), v(0) {}
v2(T _u, T _v) : u(_u), v(_v) {}
inline v2<T> operator +(const v2<T> &V) const { return v2<T>(u+V.u, v+V.v); }
inline v2<T> operator -(const v2<T> &V) const { return v2<T>(u-V.u, v-V.v); }
inline v2<T> operator *(float F) const { return v2<T>(u*F, v*F); }
template <class> friend std::ostream& operator<<(std::ostream s, v2<T>& v);
};
template <class t> struct v3 {
union {
struct {t x, y, z;};
t raw[3];
};
v3() : x(0), y(0), z(0) {}
v3(t _x, t _y, t _z) : x(_x), y(_y), z(_z) {}
inline v3<t> operator ^(const v3<t> &V) const { return v3<t>(y*V.z-z*V.y, z*V.x-x*V.z, x*V.y-y*V.x); }
inline v3<t> operator +(const v3<t> &V) const { return v3<t>(x+V.x, y+V.y, z+V.z); }
inline v3<t> operator -(const v3<t> &V) const { return v3<t>(x-V.x, y-V.y, z-V.z); }
inline v3<t> operator *(float F) const { return v3<t>(x*F, y*F, z*F); }
inline t operator *(const v3<t> &V) const { return x*V.x + y*V.y + z*V.z; }
float norm () const { return std::sqrt(x*x+y*y+z*z); }
v3<t> & normalize(t l=1) { *this = (*this)*(l/norm()); return *this; }
template <class> friend std::ostream& operator<<(std::ostream& s, v3<t>& v);
};
typedef v2<GLfloat> v2f;
typedef v2<GLfloat> t2f;
// typedef v2<float> v2f;
typedef v2<int> v2i;
typedef v3<float> v3f;
typedef v3<int> v3i;
template <class t> std::ostream& operator<<(std::ostream& s, v2<t>& v) {
s << "(" << v.x << ", " << v.y << ")\n";
return s;
}
template <class t> std::ostream& operator<<(std::ostream& s, v3<t>& v) {
s << "(" << v.x << ", " << v.y << ", " << v.z << ")\n";
return s;
}
struct VertexData2D {
v2f position;
t2f texture_coordinate;
};
/* OPENGL THINGS */
struct FRect {
GLfloat x;
GLfloat y;
GLfloat w;
GLfloat h;
};
/* OBJ FORMAT*/
class Model {
public:
Model(const char *filename);
~Model();
int num_vertexes();
int num_faces();
v3f vertex(int index);
std::vector<int> face(int index);
private:
std::vector<v3f> vertexes;
std::vector<std::vector<int>> faces;
};
Model::Model(const char *filename) : vertexes(), faces() {
std::ifstream in;
in.open (filename, std::ifstream::in);
if (in.fail()) return;
std::string line;
while (!in.eof()) {
std::getline(in, line);
std::istringstream iss(line.c_str());
char trash;
if (!line.compare(0, 2, "v ")) {
iss >> trash;
v3f v;
for (int i=0; i<3; i++) iss >> v.raw[i];
vertexes.push_back(v);
} else if (!line.compare(0, 2, "f ")) {
std::vector<int> f;
int itrash, idx;
iss >> trash;
while (iss >> idx >> trash >> itrash >> trash >> itrash) {
idx--;
f.push_back(idx);
}
faces.push_back(f);
}
}
std::cerr << "# v# " << vertexes.size() << " f# " << faces.size() << std::endl;
}
Model::~Model() {}
int Model::num_vertexes() {
return (int) vertexes.size();
}
int Model::num_faces() {
return (int) faces.size();
}
std::vector<int> Model::face(int index) {
return faces[index];
}
v3f Model::vertex(int index) {
return vertexes[index];
}
/* TIMER CLASS */
class Timer {
public:
Timer();
~Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
private:
int start_ticks;
int paused_ticks;
bool paused;
bool started;
};
Timer::Timer() {
start_ticks = 0;
paused_ticks = 0;
paused = false;
started = false;
}
Timer::~Timer() {}
void Timer::start() {
started = true;
paused = false;
start_ticks = SDL_GetTicks();
paused_ticks = 0;
}
void Timer::stop() {
started = false;
paused = false;
start_ticks = 0;
paused_ticks = 0;
}
void Timer::pause() {
if (started && !paused) {
paused = true;
paused_ticks = SDL_GetTicks() - start_ticks;
start_ticks = 0;
}
}
void Timer::unpause() {
if (started && paused) {
paused = false;
start_ticks = SDL_GetTicks() - paused_ticks;
paused_ticks = 0;
}
}
int Timer::get_ticks() {
int time = 0; //stopped time
if (started) {
if (paused) {
time = paused_ticks; //time when paused
} else {
time = SDL_GetTicks() - start_ticks; //delta from start to now
}
}
return time;
}
bool Timer::is_started() {
return started;
}
bool Timer::is_paused() {
return paused && started;
}
/* TEXTURE CLASSES */
GLenum DEFAULT_TEXTURE_WRAP = GL_REPEAT;
class Texture {
public:
Texture();
virtual ~Texture();
bool load_texture_from_file(std::string path);
bool load_texture_from_file_with_colorkey(std::string path, GLubyte r, GLubyte g, GLubyte b, GLubyte a=000);
bool load_pixels_from_file(std::string path);
bool load_texture_from_pixels(GLuint* pixels, GLuint i_width, GLuint i_height, GLuint t_width, GLuint t_height);
bool load_texture_from_pixels();
// bool load_from_rendered_text(std::string text, SDL_Color color = WHITE);
virtual void free_texture();
void render(GLfloat x, GLfloat y, FRect* clip = nullptr);
GLuint get_texture_id();
GLuint get_width();
GLuint get_height();
GLuint get_image_width();
GLuint get_image_height();
bool lock();
bool unlock();
GLuint* get_pixel_data();
GLuint get_pixel(GLuint x, GLuint y);
void set_pixel(GLuint x, GLuint y, GLuint pixel);
protected:
GLuint power_of_two(GLuint number);
GLenum detect_format(SDL_Surface* surface);
void init_VBO();
void free_VBO();
std::string file_path;
GLuint texture_id;
GLuint* raw_pixels;
GLuint texture_width;
GLuint texture_height;
GLuint image_width;
GLuint image_height;
GLuint VBO_id;
GLuint IBO_id;
GLenum pixel_mode;
};
Texture::Texture() {
texture_id = 0;
raw_pixels = nullptr;
image_width = 0;
image_height = 0;
texture_width = 0;
texture_height = 0;
VBO_id = 0;
IBO_id = 0;
pixel_mode = 0;
}
Texture::~Texture() {
free_texture();
free_VBO();
}
void Texture::free_texture() {
if (texture_id != 0) {
glDeleteTextures(1, &texture_id);
texture_id = 0;
}
if (raw_pixels != nullptr) {
delete[] raw_pixels;
raw_pixels = nullptr;
pixel_mode = 0;
}
image_width = 0;
image_height = 0;
texture_width = 0;
texture_height = 0;
}
bool Texture::lock() {
//If we have member pixels, it means we've already locked the texture
if (raw_pixels == nullptr && texture_id != 0) {
//Alloc memory for texture data
GLuint size = texture_width * texture_height;
raw_pixels = new GLuint[size];
//Set current texture
glBindTexture(GL_TEXTURE_2D, texture_id);
//Get pixels
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw_pixels);
//Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
return false;
}
bool Texture::unlock() {
//If texture is locked, and it exists
if (raw_pixels != nullptr && texture_id != 0) {
//Set current texture
glBindTexture(GL_TEXTURE_2D, texture_id);
//Update texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture_width, texture_height, GL_RGBA, GL_UNSIGNED_BYTE, raw_pixels);
//Delete pixels
delete[] raw_pixels;
raw_pixels = nullptr;
//Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
return false;
}
void Texture::init_VBO() {
//texture loaded and VBO doesn't exist
if (texture_id != 0 && VBO_id == 0) {
//Vertex data
VertexData2D vertex_data [4];
GLuint index_data[4];
//set rendering indicies
index_data[0] = 0;
index_data[1] = 1;
index_data[2] = 2;
index_data[3] = 3;
//Create VBO
glGenBuffers(1, &VBO_id);
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
glBufferData(GL_ARRAY_BUFFER, 4*sizeof(VertexData2D), vertex_data, GL_DYNAMIC_DRAW);
//Create IBO
glGenBuffers(1, &IBO_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4*sizeof(GLfloat), index_data, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
}
void Texture::free_VBO() {
if (VBO_id != 0) {
glDeleteBuffers(1, &VBO_id);
glDeleteBuffers(1, &IBO_id);
}
}
GLuint* Texture::get_pixel_data() {
return raw_pixels;
}
GLuint Texture::get_pixel(GLuint x, GLuint y) {
return raw_pixels[y*texture_width+x];
}
void Texture::set_pixel(GLuint x, GLuint y, GLuint pixel) {
raw_pixels[y*texture_width+x] = pixel;
}
GLuint Texture::power_of_two(GLuint number) {
// what black magic is this?
if (number != 0) {
number--;
number |= (number >>1);
number |= (number >>2);
number |= (number >>4);
number |= (number >>8);
number |= (number >>16);
number++;
}
return number;
}
GLenum Texture::detect_format(SDL_Surface* surface) {
// std::cout << (int)surface->format->BytesPerPixel << " bytepp " << (int)surface->format->BitsPerPixel << " bitpp" << std::endl;
if (surface->format->BytesPerPixel == 4) {
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
// std::cout << "GL_BGRA" << std::endl;
return GL_BGRA;
} else {
// std::cout << "GL_RGBA" << std::endl;
return GL_RGBA;
}
} else if (surface->format->BytesPerPixel == 3) {
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
// std::cout << "GL_BGR" << std::endl;
return GL_BGR;
} else {
// std::cout << "GL_RGB" << std::endl;
return GL_RGB;
}
} else if (surface->format->BytesPerPixel == 2) {
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
// std::cout << "GL_BGRA" << std::endl;
return GL_BGRA;
} else {
// std::cout << "GL_RGBA" << std::endl;
return GL_RGBA;
}
} else if (surface->format->BytesPerPixel == 1) {
if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
// std::cout << "GL_BGR" << std::endl;
return GL_BGR;
} else {
// std::cout << "GL_RGB" << std::endl;
return GL_RGB;
}
} else {
// std::cout << "Format not recognized!" << std::endl;
return 0;
}
}
bool Texture::load_texture_from_pixels() {
bool success = true;
if (texture_id == 0 && raw_pixels != nullptr) {
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, pixel_mode, texture_width, texture_height, 0, pixel_mode, GL_UNSIGNED_BYTE, raw_pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, DEFAULT_TEXTURE_WRAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, DEFAULT_TEXTURE_WRAP);
glBindTexture(GL_TEXTURE_2D, 0);
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
logGLError(std::cout, "Error loading texture from pixels!", error);
success = false;
} else {
//release pixels
delete[] raw_pixels;
raw_pixels = NULL;
//Generate VBO - I added this call...
init_VBO();
}
} else {
std::cout << "Cannot load texture from current pixels! ";
if (texture_id != 0) {
std::cout << "A texture is already loaded!" << std::endl;
} else if (raw_pixels == nullptr) {
std::cout << "No pixels to create texture from!" << std::endl;
}
success = false;
}
return success;
}
bool Texture::load_texture_from_pixels(GLuint* pixels, GLuint i_width, GLuint i_height, GLuint t_width, GLuint t_height) {
bool success = true;
free_texture();
image_width = i_width;
image_height = i_height;
texture_width = t_width;
texture_height = t_height;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, pixel_mode, texture_width, texture_height, 0, pixel_mode, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, DEFAULT_TEXTURE_WRAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, DEFAULT_TEXTURE_WRAP);
glBindTexture(GL_TEXTURE_2D, 0);
GLenum error = glGetError();
if (error != GL_NO_ERROR) {
logGLError(std::cout, "Error loading texture from pixels!", error);
success = false;
}
//Generate VBO
init_VBO();
return success;
}
bool Texture::load_texture_from_file(std::string path) {
bool success = true;
free_texture();
file_path = path;
SDL_Surface* lsurface = IMG_Load(path.c_str());
if (lsurface == nullptr){
logSDLError(std::cout, "IMG_Load");
} else {
SDL_Surface* csurface = SDL_ConvertSurfaceFormat(lsurface, SDL_PIXELFORMAT_RGBA32, 0);
image_width = csurface->w;
image_height = csurface->h;
// Calculate texture dimensions needed
texture_width = power_of_two(image_width);
texture_height = power_of_two(image_height);
if (image_width != texture_width || image_height != texture_height) {
// std::cout << "not 2^n size image" << std::endl;
// create new surface at desired size
int bitdepth = 32;
SDL_Surface* nsurface = SDL_CreateRGBSurfaceWithFormat(0, texture_width, texture_height, bitdepth, SDL_PIXELFORMAT_RGBA32);
if (nsurface == nullptr) {
logSDLError(std::cout, "SDL_CreateRGBSurfaceWithFormat");
} else {
// copy first image into new image, at upper leftW
int blit = SDL_BlitSurface(csurface, NULL, nsurface, NULL);
if (blit != 0) {
logSDLError(std::cout, "Surface load from file blit");
}
pixel_mode = detect_format(nsurface);
success = load_texture_from_pixels((GLuint*)nsurface->pixels, image_width, image_height, texture_width, texture_height);
}
SDL_FreeSurface(nsurface);
} else {
// std::cout << "2^n size image" << std::endl;
pixel_mode = detect_format(csurface);
success = load_texture_from_pixels((GLuint*)csurface->pixels, image_width, image_height, texture_width, texture_height);
}
SDL_FreeSurface(csurface);
}
SDL_FreeSurface(lsurface);
return success;
}
bool Texture::load_texture_from_file_with_colorkey(std::string path, GLubyte r, GLubyte g, GLubyte b, GLubyte a) {
if (!load_pixels_from_file(path)) {
return false;
}
GLuint size = texture_width * texture_height;
for (int i = 0; i < size; i++) {
GLubyte* colors = (GLubyte*)&raw_pixels[i];
if(colors[0] == r && colors[1] == g && colors[2] == b && (0 == a || colors[3] == a)) {
// make transparent
colors[0] = 255;
colors[1] = 255;
colors[2] = 255;
colors[3] = 000;
}
}
return load_texture_from_pixels();
}
bool Texture::load_pixels_from_file(std::string path) {
bool success = true;
free_texture();
file_path = path;
SDL_Surface* lsurface = IMG_Load(path.c_str());
if (lsurface == nullptr){
logSDLError(std::cout, "IMG_Load");
} else {
SDL_Surface* csurface = SDL_ConvertSurfaceFormat(lsurface, SDL_PIXELFORMAT_RGBA32, 0);
//TODO: fix this so we always are in 32 bit RGBA alpha textures
image_width = csurface->w;
image_height = csurface->h;
// Calculate texture dimensions needed
texture_width = power_of_two(image_width);
texture_height = power_of_two(image_height);
GLuint size = texture_width * texture_height;
raw_pixels = new GLuint[size];
if (image_width != texture_width || image_height != texture_height) {
// create new surface at desired size
int bitdepth = 32;
SDL_Surface* nsurface = SDL_CreateRGBSurfaceWithFormat(0, texture_width, texture_height, bitdepth, SDL_PIXELFORMAT_RGBA32);
if (nsurface == nullptr) {
logSDLError(std::cout, "SDL_CreateRGBSurfaceWithFormat");
} else {
// copy first image into new image, at upper leftW
int blit = SDL_BlitSurface(csurface, NULL, nsurface, NULL);
if (blit != 0) {
logSDLError(std::cout, "Surface load from file blit");
}
pixel_mode = detect_format(nsurface);
memcpy(raw_pixels, (GLuint*)nsurface->pixels, size * nsurface->format->BytesPerPixel);
success = true;
}
SDL_FreeSurface(nsurface);
} else {
pixel_mode = detect_format(csurface);
memcpy(raw_pixels, (GLuint*)csurface->pixels, size * csurface->format->BytesPerPixel);
success = true;
}
SDL_FreeSurface(csurface);
}
SDL_FreeSurface(lsurface);
return success;
}
// bool Texture::load_from_rendered_text(std::string text, SDL_Color color) {
// free_texture();
// SDL_Surface* surf = TTF_RenderText_Blended(font, text.c_str(), color);
// if (surf == nullptr){
// logSDLError(std::cout, "TTF_RenderText");
// } else {
// texture = SDL_CreateTextureFromSurface(renderer, surf);
// if (texture == nullptr){
// logSDLError(std::cout, "CreateTexture");
// } else {
// width = surf->w;
// height = surf->h;
// }
// SDL_FreeSurface(surf);
// }
// return texture != NULL;
// }
void Texture::render(GLfloat x, GLfloat y, FRect* clip) {
if (texture_id != 0) {
//Texture coordinates
GLfloat texture_top = 0.f;
GLfloat texture_bottom = (GLfloat)image_height / (GLfloat)texture_height;
GLfloat texture_left = 0.f;
GLfloat texture_right = (GLfloat)image_width / (GLfloat)texture_width;
//Vertex coordinates
GLfloat quad_width = image_width;
GLfloat quad_height = image_height;
//Handle clipping
if (clip != nullptr) {
texture_left = clip->x / texture_width;
texture_right = (clip->x + clip->w) / texture_width;
texture_top = clip->y / texture_height;
texture_bottom = (clip->y + clip->h) / texture_height;
quad_width = clip->w;
quad_height = clip->h;
}
//Move to rendering point
glTranslatef(x, y, 0.f);
//Set vertex data
VertexData2D vertex_data[4];
//Texture coordinates
vertex_data[0].texture_coordinate.s = texture_left; vertex_data[0].texture_coordinate.t = texture_top;
vertex_data[1].texture_coordinate.s = texture_right; vertex_data[1].texture_coordinate.t = texture_top;
vertex_data[2].texture_coordinate.s = texture_right; vertex_data[2].texture_coordinate.t = texture_bottom;
vertex_data[3].texture_coordinate.s = texture_left; vertex_data[3].texture_coordinate.t = texture_bottom;
//Vertex positions
vertex_data[0].position.x = 0.f; vertex_data[0].position.y = 0.f;
vertex_data[1].position.x = quad_width; vertex_data[1].position.y = 0.f;
vertex_data[2].position.x = quad_width; vertex_data[2].position.y = quad_height;
vertex_data[3].position.x = 0.f; vertex_data[3].position.y = quad_height;
//Set texture ID
glBindTexture(GL_TEXTURE_2D, texture_id);
//Enable vertex and texture coordinate arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//Bind vertex buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO_id);
//Update vertex buffer data
glBufferSubData(GL_ARRAY_BUFFER, 0, 4*sizeof(VertexData2D), vertex_data);
//Set texture coordinate data
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData2D), (GLvoid*)offsetof(VertexData2D, texture_coordinate));
//Set vertex data
glVertexPointer(2, GL_FLOAT, sizeof(VertexData2D), (GLvoid*)offsetof(VertexData2D, position));
//Draw quad using vertex data and index data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO_id);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, NULL);
//Disable Vertex and texture coordinate arrays
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
GLuint Texture::get_texture_id() {
return texture_id;
}
GLuint Texture::get_width() {
return texture_width;
}
GLuint Texture::get_height() {
return texture_height;
}
GLuint Texture::get_image_width() {
return image_width;
}
GLuint Texture::get_image_height() {
return image_height;
}
enum SpriteOrigin {
SPRITE_ORIGIN_CENTER,
SPRITE_ORIGIN_TOP_LEFT,
SPRITE_ORIGIN_BOTTOM_LEFT,
SPRITE_ORIGIN_TOP_RIGHT,
SPRITE_ORIGIN_BOTTOM_RIGHT
};
class SpriteSheet : public Texture {
public:
SpriteSheet();
~SpriteSheet();
int add_sprite_clip(FRect& new_clip);
FRect get_clip(int index);
bool generate_data_buffer(SpriteOrigin = SPRITE_ORIGIN_CENTER);
void free_sheet();
void free_texture();
void render_sprite(int index);
protected:
std::vector<FRect> clips;
GLuint vertex_data_buffer;
GLuint* index_buffers;
};
SpriteSheet::SpriteSheet() {
vertex_data_buffer = (GLuint)NULL;
index_buffers = NULL;
}
SpriteSheet::~SpriteSheet() {
free_sheet();
}
int SpriteSheet::add_sprite_clip(FRect& new_clip) {
clips.push_back(new_clip);
return clips.size() - 1;
}
FRect SpriteSheet::get_clip(int index) {
return clips[index];
}
bool SpriteSheet::generate_data_buffer(SpriteOrigin origin) {
if (get_texture_id() != 0 && clips.size() > 0) {
int total_sprites = clips.size();
VertexData2D* vertex_data = new VertexData2D[total_sprites*4];
index_buffers = new GLuint[total_sprites];
glGenBuffers(1, &vertex_data_buffer);
glGenBuffers(total_sprites, index_buffers);
GLfloat tex_width = get_width();
GLfloat tex_height = get_height();
GLuint sprite_indicies[4] = {0,0,0,0};
//for origin calculation
GLfloat vertex_top = 0.f;
GLfloat vertex_bottom = 0.f;
GLfloat vertex_left = 0.f;
GLfloat vertex_right = 0.f;
for (int i = 0; i < total_sprites; i++) {
sprite_indicies[0] = i*4+0;
sprite_indicies[1] = i*4+1;
sprite_indicies[2] = i*4+2;
sprite_indicies[3] = i*4+3;
//set origin
switch (origin) {
case SPRITE_ORIGIN_TOP_LEFT:
vertex_top = 0.f;
vertex_bottom = clips[i].h;
vertex_left = 0.f;
vertex_right = clips[i].w;
break;
case SPRITE_ORIGIN_TOP_RIGHT:
vertex_top = 0.f;
vertex_bottom = clips[i].h;
vertex_left = -clips[i].w;
vertex_right = 0.f;
break;
case SPRITE_ORIGIN_BOTTOM_LEFT:
vertex_top = -clips[i].h;
vertex_bottom = 0.f;
vertex_left = 0.f;
vertex_right = clips[i].w;
break;
case SPRITE_ORIGIN_BOTTOM_RIGHT:
vertex_top = -clips[i].h;
vertex_bottom = 0.f;
vertex_left = -clips[i].w;
vertex_right = 0.f;
break;
default: //SPRITE_ORIGIN_CENTER
vertex_top = -clips[i].h / 2.f;
vertex_bottom = clips[i].h / 2.f;
vertex_left = -clips[i].w / 2.f;
vertex_right = clips[i].w / 2.f;
break;
}
vertex_data[sprite_indicies[0]].position.x = vertex_left;
vertex_data[sprite_indicies[0]].position.y = vertex_top;
vertex_data[sprite_indicies[0]].texture_coordinate.s = (clips[i].x) / tex_width;
vertex_data[sprite_indicies[0]].texture_coordinate.t = (clips[i].y) / tex_height;
vertex_data[sprite_indicies[1]].position.x = vertex_right;
vertex_data[sprite_indicies[1]].position.y = vertex_top;
vertex_data[sprite_indicies[1]].texture_coordinate.s = (clips[i].x + clips[i].w) / tex_width;
vertex_data[sprite_indicies[1]].texture_coordinate.t = (clips[i].y) / tex_height;
vertex_data[sprite_indicies[2]].position.x = vertex_right;
vertex_data[sprite_indicies[2]].position.y = vertex_bottom;
vertex_data[sprite_indicies[2]].texture_coordinate.s = (clips[i].x + clips[i].w) / tex_width;
vertex_data[sprite_indicies[2]].texture_coordinate.t = (clips[i].y + clips[i].h) / tex_height;
vertex_data[sprite_indicies[3]].position.x = vertex_left;
vertex_data[sprite_indicies[3]].position.y = vertex_bottom;
vertex_data[sprite_indicies[3]].texture_coordinate.s = (clips[i].x) / tex_width;
vertex_data[sprite_indicies[3]].texture_coordinate.t = (clips[i].y + clips[i].h) / tex_height;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffers[i]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4*sizeof(GLuint), sprite_indicies, GL_STATIC_DRAW);
}
glBindBuffer(GL_ARRAY_BUFFER, vertex_data_buffer);
glBufferData(GL_ARRAY_BUFFER, total_sprites*4*sizeof(VertexData2D), vertex_data, GL_STATIC_DRAW);
delete[] vertex_data;
} else {
if (get_texture_id() == 0) {
printf("No texture to render with!\n");
}
if (clips.size() <= 0) {
printf("No clips to generate vertex data from!\n");
}
return false;
}
return true;
}
void SpriteSheet::free_sheet() {
if (vertex_data_buffer != (GLuint)NULL) {
glDeleteBuffers(1, &vertex_data_buffer);
vertex_data_buffer = (GLuint)NULL;
}
if (index_buffers != NULL) {
glDeleteBuffers(clips.size(), index_buffers);
delete[] index_buffers;
index_buffers = NULL;
}
clips.clear();
}
void SpriteSheet::free_texture() {
free_sheet();
Texture::free_texture();
}
void SpriteSheet::render_sprite(int index) {
if (vertex_data_buffer != (GLuint)NULL) {
glBindTexture(GL_TEXTURE_2D, get_texture_id());
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vertex_data_buffer);
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexData2D), (GLvoid*)offsetof(VertexData2D, texture_coordinate));
glVertexPointer(2, GL_FLOAT, sizeof(VertexData2D), (GLvoid*)offsetof(VertexData2D, position));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffers[index]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, NULL);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
}
class Font : private SpriteSheet {
public:
Font();
~Font();
bool load_bitmap(std::string path);
void free_font();
void render_text(GLfloat x, GLfloat y, std::string text);
private:
GLfloat space;
GLfloat line_height;
GLfloat new_line;
};
Font::Font() {
space = 0.f;
line_height = 0.f;
new_line = 0.f;
}
Font::~Font() {
free_font();
}
void Font::free_font() {
free_texture();
space = 0.f;
line_height = 0.f;
new_line = 0.f;
}
bool Font::load_bitmap(std::string path) {
//expects path to bitmap font image with black, white, shades of grey, black is background color
//arranged in 16x16 grid in ASCII order
bool success = true;
const GLuint BLACK_PIXEL = 0xFF000000;
free_font();
if (load_pixels_from_file(path)) {
//cell dimensions
GLfloat cell_width = get_image_width() / 16.f;
GLfloat cell_height = get_image_height() / 16.f;
//letter top and bottom
GLuint top = cell_height;
GLuint bottom = 0;
GLuint A_bottom = 0;
//current pixel coordinates
int pixel_x = 0;
int pixel_y = 0;
//base cell offsets
int base_offset_x = 0;
int base_offset_y = 0;
//parsing bitmap
GLuint current_char = 0;
FRect next_clip = {0.f, 0.f, cell_width, cell_height};
//iterate cell rows
for (unsigned int rows = 0; rows < 16; rows++) {
for (unsigned int columns = 0; columns < 16; columns++) {
//parsing cell
base_offset_x = cell_width * columns;
base_offset_y = cell_height * rows;
next_clip.x = cell_width * columns;
next_clip.y = cell_height * rows;
next_clip.w = cell_width;
next_clip.h = cell_height;
//find left side of character
for (int pixel_column = 0; pixel_column < cell_width; pixel_column++) {
for (int pixel_row = 0; pixel_row < cell_height; pixel_row++) {
pixel_x = base_offset_x + pixel_column;
pixel_y = base_offset_y + pixel_row;
if (get_pixel(pixel_x, pixel_y) != BLACK_PIXEL) {
// set sprite x offset
next_clip.x = pixel_x;
// break loop
pixel_column = cell_width;
pixel_row = cell_height;
}
}
}
//find right side of character (width)
for (int pixel_column_width = cell_width - 1; pixel_column_width >= 0; pixel_column_width--) {
for (int pixel_row_width = 0; pixel_row_width < cell_height; pixel_row_width++) {
pixel_x = base_offset_x + pixel_column_width;
pixel_y = base_offset_y + pixel_row_width;
if (get_pixel(pixel_x, pixel_y) != BLACK_PIXEL) {
// set sprite width
next_clip.w = (pixel_x - next_clip.x) + 1;
// break loop
pixel_column_width = -1;
pixel_row_width = cell_height;
}
}
}
//find top side of character
for (int pixel_row = 0; pixel_row < cell_height; pixel_row++) {
for (int pixel_column = 0; pixel_column < cell_width; pixel_column++) {
pixel_x = base_offset_x + pixel_column;
pixel_y = base_offset_y + pixel_row;
if (get_pixel(pixel_x, pixel_y) != BLACK_PIXEL) {
// new top found
if (pixel_row < top) {
top = pixel_row;
}
//break loops
pixel_column = cell_width;
pixel_row = cell_height;
}
}
}
//find bottom side of character (height) and baseline A
for (int pixel_row_base = cell_height - 1; pixel_row_base >= 0; pixel_row_base--) {
for (int pixel_column_base = 0; pixel_column_base < cell_width; pixel_column_base++) {
pixel_x = base_offset_x + pixel_column_base;
pixel_y = base_offset_y + pixel_row_base;
if (get_pixel(pixel_x, pixel_y) != BLACK_PIXEL) {
// set baseline
if (current_char == 'A') {
A_bottom = pixel_row_base;
}
// new bottom
if (pixel_row_base > bottom) {
bottom = pixel_row_base;
}
// break loop
pixel_column_base = cell_width;
pixel_row_base = -1;
}
}
}
clips.push_back(next_clip);
current_char++;
}
}
//trip excess height from top of fonts
for (int t = 0; t < 256; t++) {