00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "Core/precomp.h"
00016 #ifdef USE_OPENGL
00017
00018 #include "blit_gl_generic.h"
00019 #include "Display/Display/Generic/pixeldata.h"
00020
00021
00022 void check4error()
00023 {
00024 GLenum error;
00025 while ((error = glGetError()) != GL_NO_ERROR)
00026 std::cout << gluErrorString(error) << std::endl;
00027 }
00028
00029 CL_Blit_GLTexture::CL_Blit_GLTexture(
00030 CL_GL_DisplayCard_Generic *card,
00031 CL_SurfaceProvider *provider)
00032 {
00033 card->begin_2d();
00034
00035 this->card = card;
00036 provider->lock();
00037
00038 width = provider->get_width();
00039 height = provider->get_height();
00040 no_sprs = provider->get_num_frames();
00041
00042 int texture_size = 1;
00043 while (texture_size < width || texture_size < height*no_sprs) texture_size *= 2;
00044
00045 texture_width = texture_size;
00046 texture_height = texture_size;
00047
00048 cl_assert(width <= texture_width);
00049 cl_assert(height <= texture_height);
00050
00051 CL_PixelData pixeldata(
00052 255,
00053 255 << 8,
00054 255 << 16,
00055 255 << 24,
00056 provider,
00057 4);
00058
00059 unsigned int *texture_data = new unsigned int[texture_width*texture_height];
00060
00061
00062
00063 if (provider->get_alpha_mask() == 0)
00064 {
00065 for (int y=0; y<height*no_sprs; y++)
00066 {
00067 unsigned int* src = (unsigned int*) pixeldata.get_line_pixel(y);
00068 unsigned int* dst = (unsigned int*) &texture_data[texture_width*y];
00069 for (int x=0; x< width; x++)
00070 {
00071 *(dst++) = (*src & 0x00FFFFFF) | (~(*src) & 0xFF000000);
00072 src++;
00073 }
00074 }
00075 }
00076 else
00077 {
00078 for (int y=0; y<height*no_sprs; y++)
00079 {
00080 memcpy(
00081 &texture_data[texture_width*y],
00082 pixeldata.get_line_pixel(y),
00083 width * sizeof(int));
00084 }
00085 }
00086
00087 glGenTextures(1,&texture);
00088 glBindTexture(GL_TEXTURE_2D, texture);
00089
00090
00091
00092
00093
00094
00095
00096 gluBuild2DMipmaps(
00097 GL_TEXTURE_2D,
00098 GL_RGBA,
00099 texture_width,
00100 texture_height,
00101 GL_RGBA,
00102 GL_UNSIGNED_BYTE,
00103 texture_data);
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 delete[] texture_data;
00118
00119 provider->unlock();
00120 card->end_2d();
00121 }
00122
00123 CL_Blit_GLTexture::~CL_Blit_GLTexture()
00124 {
00125 glDeleteTextures(1,&texture);
00126 }
00127
00128 void CL_Blit_GLTexture::blt_noclip(
00129 CL_Target *target,
00130 int x,
00131 int y,
00132 int spr_no)
00133 {
00134 card->begin_2d();
00135
00136
00137 glBindTexture(GL_TEXTURE_2D, texture);
00138 float h1 = ((float) height * spr_no) / texture_height;
00139 float h2 = ((float) height * (spr_no+1)) / texture_height;
00140 float w = (float) width / texture_width;
00141 glBegin(GL_TRIANGLE_STRIP);
00142 glTexCoord2f(0.0, h1); glVertex2i(x, y);
00143 glTexCoord2f( w, h1); glVertex2i(x + width, y);
00144 glTexCoord2f(0.0, h2); glVertex2i(x, y + height);
00145 glTexCoord2f( w, h2); glVertex2i(x + width, y + height);
00146 glEnd();
00147
00148 card->end_2d();
00149 }
00150
00151 void CL_Blit_GLTexture::blt_clip(
00152 CL_Target *target,
00153 int x,
00154 int y,
00155 int spr_no,
00156 const CL_ClipRect &clip)
00157 {
00158
00159 blt_noclip(target,x,y,spr_no);
00160 }
00161
00162 void CL_Blit_GLTexture::blt_scale_noclip(
00163 CL_Target *target,
00164 int x,
00165 int y,
00166 int dest_width,
00167 int dest_height,
00168 int spr_no)
00169 {
00170 card->begin_2d();
00171
00172 glBindTexture(GL_TEXTURE_2D, texture);
00173 float h1 = ((float) height * spr_no) / texture_height;
00174 float h2 = ((float) height * (spr_no+1)) / texture_height;
00175 float w = (float) width / texture_width;
00176 glBegin(GL_TRIANGLE_STRIP);
00177 glTexCoord2f(0.0, h1); glVertex2i(x, y);
00178 glTexCoord2f( w, h1); glVertex2i(x + dest_width, y);
00179 glTexCoord2f(0.0, h2); glVertex2i(x, y + dest_height);
00180 glTexCoord2f( w, h2); glVertex2i(x + dest_width, y + dest_height);
00181 glEnd();
00182
00183 card->end_2d();
00184 }
00185
00186 void CL_Blit_GLTexture::blt_scale_clip(
00187 CL_Target *target,
00188 int x,
00189 int y,
00190 int dest_width,
00191 int dest_height,
00192 int spr_no,
00193 const CL_ClipRect &clip)
00194 {
00195
00196 blt_scale_noclip(target, x, y, dest_width, dest_height, spr_no);
00197 }
00198
00199 #endif