00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032
00033
00034
00035
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039
00040 #include "stratagus.h"
00041 #include "video.h"
00042
00043
00044
00045
00046
00047 void DrawTexture(const CGraphic *g, GLuint *textures, int sx, int sy,
00048 int ex, int ey, int x, int y, int flip)
00049 {
00050 GLfloat stx, etx;
00051 GLfloat sty, ety;
00052 int texture;
00053 int minw, minh;
00054 int maxw, maxh;
00055 int i, j;
00056 int tw, th;
00057 int sx2, sy2;
00058 int ex2, ey2;
00059 int w, h;
00060 int x2, y2;
00061 int nextsx2, nextsy2;
00062
00063 tw = ex / GLMaxTextureSize - sx / GLMaxTextureSize + 1;
00064 th = ey / GLMaxTextureSize - sy / GLMaxTextureSize + 1;
00065
00066 x2 = x;
00067 y2 = y;
00068
00069 sy2 = sy;
00070 for (j = 0; j < th; ++j) {
00071 minh = sy2 / GLMaxTextureSize * GLMaxTextureSize;
00072 maxh = std::min((const int)(minh + GLMaxTextureSize), g->GraphicHeight);
00073 if (sy > minh) {
00074 h = ey - sy;
00075 } else {
00076 h = ey - minh;
00077 }
00078 if (h > maxh) {
00079 h = maxh;
00080 }
00081
00082 sx2 = sx;
00083 for (i = 0; i < tw; ++i) {
00084 minw = sx2 / GLMaxTextureSize * GLMaxTextureSize;
00085 maxw = std::min((const int)(minw + GLMaxTextureSize), g->GraphicWidth);
00086 if (sx > minw) {
00087 w = ex - sx;
00088 } else {
00089 w = ex - minw;
00090 }
00091 if (w > maxw) {
00092 w = maxw;
00093 }
00094
00095 stx = (GLfloat)(sx2 - minw) / (maxw - minw);
00096 sty = (GLfloat)(sy2 - minh) / (maxh - minh);
00097 if (ex > maxw) {
00098 ex2 = maxw;
00099 } else {
00100 ex2 = ex;
00101 }
00102 etx = (GLfloat)(ex2 - sx2);
00103 if (maxw == g->GraphicWidth) {
00104 stx *= g->TextureWidth;
00105 etx = stx + etx / (maxw - minw) * g->TextureWidth;
00106 } else {
00107 etx = stx + (etx / GLMaxTextureSize);
00108 }
00109 if (ey > maxh) {
00110 ey2 = maxh;
00111 } else {
00112 ey2 = ey;
00113 }
00114 ety = (GLfloat)(ey2 - sy2);
00115 if (maxh == g->GraphicHeight) {
00116 sty *= g->TextureHeight;
00117 ety = sty + ety / (maxh - minh) * g->TextureHeight;
00118 } else {
00119 ety = sty + (ety / GLMaxTextureSize);
00120 }
00121
00122 texture = sy2 / GLMaxTextureSize * ((g->GraphicWidth - 1) / GLMaxTextureSize + 1) +
00123 sx2 / GLMaxTextureSize;
00124
00125 glBindTexture(GL_TEXTURE_2D, textures[texture]);
00126 glBegin(GL_QUADS);
00127 if (!flip) {
00128 glTexCoord2f(stx, sty);
00129 glVertex2i(x2, y2);
00130 glTexCoord2f(stx, ety);
00131 glVertex2i(x2, y2 + h);
00132 glTexCoord2f(etx, ety);
00133 glVertex2i(x2 + w, y2 + h);
00134 glTexCoord2f(etx, sty);
00135 glVertex2i(x2 + w, y2);
00136 } else {
00137 glTexCoord2f(stx, sty);
00138 glVertex2i(x + (ex - sx) - (x2 - x), y2);
00139 glTexCoord2f(stx, ety);
00140 glVertex2i(x + (ex - sx) - (x2 - x), y2 + h);
00141 glTexCoord2f(etx, ety);
00142 glVertex2i(x + (ex - sx) - (x2 + w - x), y2 + h);
00143 glTexCoord2f(etx, sty);
00144 glVertex2i(x + (ex - sx) - (x2 + w - x), y2);
00145 }
00146 glEnd();
00147
00148 nextsx2 = (sx2 + GLMaxTextureSize) / GLMaxTextureSize * GLMaxTextureSize;
00149 x2 += nextsx2 - sx2;
00150 sx2 = nextsx2;
00151 }
00152
00153 nextsy2 = (sy2 + GLMaxTextureSize) / GLMaxTextureSize * GLMaxTextureSize;
00154 y2 += nextsy2 - sy2;
00155 sy2 = nextsy2;
00156 }
00157 }
00158