supertux-nogl.diff 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. #
  2. # SuperTux -nogl patch
  3. # Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. #
  19. # -----------------------------------------------------------------------------
  20. #
  21. # This patch allows running the game on systems without OpenGL support.
  22. #
  23. # It modifies the video portion of the SuperTux engine to render all graphics
  24. # with SDL functions only. Many features are removed from the video engine,
  25. # so don't expect much.
  26. #
  27. # Installing the patch should be pretty straightforward. Simply run the
  28. # following command prior to running autogen.sh and configure:
  29. #
  30. # patch -p0 < contrib/supertux-nogl.diff
  31. #
  32. # This patch works for revision 5072. It may break for later revisions.
  33. #
  34. # -----------------------------------------------------------------------------
  35. Index: src/gameconfig.hpp
  36. ===================================================================
  37. --- src/gameconfig.hpp (revision 5071)
  38. +++ src/gameconfig.hpp (working copy)
  39. @@ -19,6 +19,8 @@
  40. #ifndef SUPERTUX_CONFIG_H
  41. #define SUPERTUX_CONFIG_H
  42. +#include <config.h>
  43. +
  44. #include <string>
  45. class Config
  46. @@ -39,6 +41,9 @@
  47. float aspect_ratio;
  48. bool use_fullscreen;
  49. +#ifdef HAVE_OPENGL
  50. + bool use_opengl;
  51. +#endif
  52. bool try_vsync;
  53. bool show_fps;
  54. bool sound_enabled;
  55. Index: src/video/drawing_context.cpp
  56. ===================================================================
  57. --- src/video/drawing_context.cpp (revision 5071)
  58. +++ src/video/drawing_context.cpp (working copy)
  59. @@ -23,17 +23,16 @@
  60. #include <cassert>
  61. #include <iostream>
  62. #include <SDL_image.h>
  63. -#include <GL/gl.h>
  64. #include <sstream>
  65. #include <iomanip>
  66. #include <physfs.h>
  67. +#include "glutil.hpp"
  68. #include "drawing_context.hpp"
  69. #include "surface.hpp"
  70. #include "font.hpp"
  71. #include "main.hpp"
  72. #include "gameconfig.hpp"
  73. -#include "glutil.hpp"
  74. #include "texture.hpp"
  75. #include "texture_manager.hpp"
  76. #include "obstack/obstackpp.hpp"
  77. @@ -113,6 +112,7 @@
  78. {
  79. screen = SDL_GetVideoSurface();
  80. +#ifdef HAVE_OPENGL
  81. lightmap_width = screen->w / LIGHTMAP_DIV;
  82. lightmap_height = screen->h / LIGHTMAP_DIV;
  83. unsigned int width = next_po2(lightmap_width);
  84. @@ -123,6 +123,7 @@
  85. lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
  86. lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
  87. texture_manager->register_texture(lightmap);
  88. +#endif
  89. requests = &drawing_requests;
  90. @@ -133,8 +134,10 @@
  91. {
  92. obstack_free(&obst, NULL);
  93. +#ifdef HAVE_OPENGL
  94. texture_manager->remove_texture(lightmap);
  95. delete lightmap;
  96. +#endif
  97. }
  98. void
  99. @@ -338,15 +341,31 @@
  100. const GetLightRequest* getlightrequest
  101. = (GetLightRequest*) request.request_data;
  102. - float pixels[3];
  103. - for( int i = 0; i<3; i++)
  104. - pixels[i] = 0.0f; //set to black
  105. +#ifdef HAVE_OPENGL
  106. + if(config->use_opengl)
  107. + {
  108. + float pixels[3];
  109. + for( int i = 0; i<3; i++)
  110. + pixels[i] = 0.0f; //set to black
  111. - float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
  112. - float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
  113. - glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
  114. + float posX = request.pos.x * lightmap_width / SCREEN_WIDTH;
  115. + float posY = screen->h - request.pos.y * lightmap_height / SCREEN_HEIGHT;
  116. + glReadPixels((GLint) posX, (GLint) posY , 1, 1, GL_RGB, GL_FLOAT, pixels);
  117. *(getlightrequest->color_ptr) = Color( pixels[0], pixels[1], pixels[2]);
  118. - //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
  119. + //printf("get_light %f/%f =>%f/%f r%f g%f b%f\n", request.pos.x, request.pos.y, posX, posY, pixels[0], pixels[1], pixels[2]);
  120. + }
  121. + else
  122. +#endif
  123. + {
  124. + // FIXME: implement properly for SDL
  125. + static int i = 0;
  126. + i += 1; i &= 0xFFFF;
  127. + if (i & 0x8000) {
  128. + *(getlightrequest->color_ptr) = Color(0.0f, 0.0f, 0.0f);
  129. + } else {
  130. + *(getlightrequest->color_ptr) = Color(1.0f, 1.0f, 1.0f);
  131. + }
  132. + }
  133. }
  134. void
  135. @@ -370,17 +389,50 @@
  136. const Color& top = gradientrequest->top;
  137. const Color& bottom = gradientrequest->bottom;
  138. - glDisable(GL_TEXTURE_2D);
  139. - glBegin(GL_QUADS);
  140. - glColor4f(top.red, top.green, top.blue, top.alpha);
  141. - glVertex2f(0, 0);
  142. - glVertex2f(SCREEN_WIDTH, 0);
  143. - glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
  144. - glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
  145. - glVertex2f(0, SCREEN_HEIGHT);
  146. - glEnd();
  147. - glEnable(GL_TEXTURE_2D);
  148. - glColor4f(1, 1, 1, 1);
  149. +#ifdef HAVE_OPENGL
  150. + if(config->use_opengl)
  151. + {
  152. + glDisable(GL_TEXTURE_2D);
  153. + glBegin(GL_QUADS);
  154. + glColor4f(top.red, top.green, top.blue, top.alpha);
  155. + glVertex2f(0, 0);
  156. + glVertex2f(SCREEN_WIDTH, 0);
  157. + glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
  158. + glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
  159. + glVertex2f(0, SCREEN_HEIGHT);
  160. + glEnd();
  161. + glEnable(GL_TEXTURE_2D);
  162. + glColor4f(1, 1, 1, 1);
  163. + }
  164. + else
  165. +#endif
  166. + {
  167. + for(int y = 0;y < screen->h;++y)
  168. + {
  169. + Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
  170. + Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
  171. + Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
  172. + Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
  173. + Uint32 color = SDL_MapRGB(screen->format, r, g, b);
  174. +
  175. + SDL_Rect rect;
  176. + rect.x = 0;
  177. + rect.y = y;
  178. + rect.w = screen->w;
  179. + rect.h = 1;
  180. +
  181. + if(a == SDL_ALPHA_OPAQUE) {
  182. + SDL_FillRect(screen, &rect, color);
  183. + } else if(a != SDL_ALPHA_TRANSPARENT) {
  184. + SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
  185. +
  186. + SDL_FillRect(temp, 0, color);
  187. + SDL_SetAlpha(temp, SDL_SRCALPHA, a);
  188. + SDL_BlitSurface(temp, 0, screen, &rect);
  189. + SDL_FreeSurface(temp);
  190. + }
  191. + }
  192. + }
  193. }
  194. void
  195. @@ -398,29 +450,76 @@
  196. const FillRectRequest* fillrectrequest
  197. = (FillRectRequest*) request.request_data;
  198. - float x = request.pos.x;
  199. - float y = request.pos.y;
  200. - float w = fillrectrequest->size.x;
  201. - float h = fillrectrequest->size.y;
  202. +#ifdef HAVE_OPENGL
  203. + if(config->use_opengl)
  204. + {
  205. + float x = request.pos.x;
  206. + float y = request.pos.y;
  207. + float w = fillrectrequest->size.x;
  208. + float h = fillrectrequest->size.y;
  209. - glDisable(GL_TEXTURE_2D);
  210. - glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
  211. - fillrectrequest->color.blue, fillrectrequest->color.alpha);
  212. + glDisable(GL_TEXTURE_2D);
  213. + glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
  214. + fillrectrequest->color.blue, fillrectrequest->color.alpha);
  215. - glBegin(GL_QUADS);
  216. - glVertex2f(x, y);
  217. - glVertex2f(x+w, y);
  218. - glVertex2f(x+w, y+h);
  219. - glVertex2f(x, y+h);
  220. - glEnd();
  221. - glEnable(GL_TEXTURE_2D);
  222. -
  223. - glColor4f(1, 1, 1, 1);
  224. + glBegin(GL_QUADS);
  225. + glVertex2f(x, y);
  226. + glVertex2f(x+w, y);
  227. + glVertex2f(x+w, y+h);
  228. + glVertex2f(x, y+h);
  229. + glEnd();
  230. + glEnable(GL_TEXTURE_2D);
  231. + glColor4f(1, 1, 1, 1);
  232. + }
  233. + else
  234. +#endif
  235. + {
  236. + float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
  237. + float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
  238. + int numerator;
  239. + int denominator;
  240. + if(xfactor < yfactor)
  241. + {
  242. + numerator = config->screenwidth;
  243. + denominator = SCREEN_WIDTH;
  244. + }
  245. + else
  246. + {
  247. + numerator = config->screenheight;
  248. + denominator = SCREEN_HEIGHT;
  249. + }
  250. +
  251. + SDL_Rect rect;
  252. + rect.x = (Sint16)request.pos.x * numerator / denominator;
  253. + rect.y = (Sint16)request.pos.y * numerator / denominator;
  254. + rect.w = (Uint16)fillrectrequest->size.x * numerator / denominator;
  255. + rect.h = (Uint16)fillrectrequest->size.y * numerator / denominator;
  256. + Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
  257. + Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
  258. + Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
  259. + Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
  260. + Uint32 color = SDL_MapRGB(screen->format, r, g, b);
  261. + if(a == SDL_ALPHA_OPAQUE) {
  262. + SDL_FillRect(screen, &rect, color);
  263. + } else if(a != SDL_ALPHA_TRANSPARENT) {
  264. + SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
  265. +
  266. + SDL_FillRect(temp, 0, color);
  267. + SDL_SetAlpha(temp, SDL_SRCALPHA, a);
  268. + SDL_BlitSurface(temp, 0, screen, &rect);
  269. + SDL_FreeSurface(temp);
  270. + }
  271. + }
  272. }
  273. void
  274. +#ifdef HAVE_OPENGL
  275. DrawingContext::draw_lightmap(const DrawingRequest& request) const
  276. +#else
  277. +DrawingContext::draw_lightmap(const DrawingRequest&) const
  278. +#endif
  279. {
  280. +#ifdef HAVE_OPENGL
  281. const Texture* texture = reinterpret_cast<Texture*> (request.request_data);
  282. // multiple the lightmap with the framebuffer
  283. @@ -444,6 +543,7 @@
  284. glEnd();
  285. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  286. +#endif
  287. }
  288. void
  289. @@ -462,29 +562,38 @@
  290. // PART1: create lightmap
  291. if(use_lightmap) {
  292. - glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
  293. - glMatrixMode(GL_PROJECTION);
  294. - glLoadIdentity();
  295. - glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  296. - glMatrixMode(GL_MODELVIEW);
  297. - glLoadIdentity();
  298. +#ifdef HAVE_OPENGL
  299. + if(config->use_opengl)
  300. + {
  301. + glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
  302. + glMatrixMode(GL_PROJECTION);
  303. + glLoadIdentity();
  304. + glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  305. + glMatrixMode(GL_MODELVIEW);
  306. + glLoadIdentity();
  307. - glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
  308. - glClear(GL_COLOR_BUFFER_BIT);
  309. - handle_drawing_requests(lightmap_requests);
  310. - lightmap_requests.clear();
  311. + glClearColor( ambient_color.red, ambient_color.green, ambient_color.blue, 1 );
  312. + glClear(GL_COLOR_BUFFER_BIT);
  313. + handle_drawing_requests(lightmap_requests);
  314. + lightmap_requests.clear();
  315. - glDisable(GL_BLEND);
  316. - glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
  317. - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
  318. + glDisable(GL_BLEND);
  319. + glBindTexture(GL_TEXTURE_2D, lightmap->get_handle());
  320. + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
  321. - glViewport(0, 0, screen->w, screen->h);
  322. - glMatrixMode(GL_PROJECTION);
  323. - glLoadIdentity();
  324. - glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  325. - glMatrixMode(GL_MODELVIEW);
  326. - glLoadIdentity();
  327. - glEnable(GL_BLEND);
  328. + glViewport(0, 0, screen->w, screen->h);
  329. + glMatrixMode(GL_PROJECTION);
  330. + glLoadIdentity();
  331. + glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  332. + glMatrixMode(GL_MODELVIEW);
  333. + glLoadIdentity();
  334. + glEnable(GL_BLEND);
  335. + }
  336. + else
  337. +#endif
  338. + {
  339. + // FIXME: SDL alternative
  340. + }
  341. // add a lightmap drawing request into the queue
  342. DrawingRequest* request = new(obst) DrawingRequest();
  343. @@ -499,7 +608,12 @@
  344. drawing_requests.clear();
  345. obstack_free(&obst, NULL);
  346. obstack_init(&obst);
  347. - assert_gl("drawing");
  348. +#ifdef HAVE_OPENGL
  349. + if(config->use_opengl)
  350. + {
  351. + assert_gl("drawing");
  352. + }
  353. +#endif
  354. // if a screenshot was requested, take one
  355. if (screenshot_requested) {
  356. @@ -507,7 +621,16 @@
  357. screenshot_requested = false;
  358. }
  359. - SDL_GL_SwapBuffers();
  360. +#ifdef HAVE_OPENGL
  361. + if(config->use_opengl)
  362. + {
  363. + SDL_GL_SwapBuffers();
  364. + }
  365. + else
  366. +#endif
  367. + {
  368. + SDL_Flip(screen);
  369. + }
  370. }
  371. class RequestPtrCompare
  372. @@ -644,36 +767,47 @@
  373. {
  374. // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
  375. - // create surface to hold screenshot
  376. - #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  377. - SDL_Surface* shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
  378. - #else
  379. - SDL_Surface* shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
  380. - #endif
  381. - if (!shot_surf) {
  382. - log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
  383. - return;
  384. - }
  385. + SDL_Surface *shot_surf;
  386. +#ifdef HAVE_OPENGL
  387. + if(config->use_opengl)
  388. + {
  389. + // create surface to hold screenshot
  390. + #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  391. + shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
  392. + #else
  393. + shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
  394. + #endif
  395. + if (!shot_surf) {
  396. + log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
  397. + return;
  398. + }
  399. - // read pixels into array
  400. - char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
  401. - if (!pixels) {
  402. - log_warning << "Could not allocate memory to store screenshot" << std::endl;
  403. - SDL_FreeSurface(shot_surf);
  404. - return;
  405. - }
  406. - glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  407. + // read pixels into array
  408. + char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
  409. + if (!pixels) {
  410. + log_warning << "Could not allocate memory to store screenshot" << std::endl;
  411. + SDL_FreeSurface(shot_surf);
  412. + return;
  413. + }
  414. + glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  415. - // copy array line-by-line
  416. - for (int i = 0; i < SCREEN_HEIGHT; i++) {
  417. - char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
  418. - char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
  419. - memcpy(dst, src, 3 * SCREEN_WIDTH);
  420. + // copy array line-by-line
  421. + for (int i = 0; i < SCREEN_HEIGHT; i++) {
  422. + char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
  423. + char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
  424. + memcpy(dst, src, 3 * SCREEN_WIDTH);
  425. + }
  426. +
  427. + // free array
  428. + delete[](pixels);
  429. }
  430. + else
  431. +#endif
  432. + {
  433. + shot_surf = SDL_GetVideoSurface();
  434. + shot_surf->refcount++;
  435. + }
  436. - // free array
  437. - delete[](pixels);
  438. -
  439. // save screenshot
  440. static const std::string writeDir = PHYSFS_getWriteDir();
  441. static const std::string dirSep = PHYSFS_getDirSeparator();
  442. Index: src/video/texture_manager.hpp
  443. ===================================================================
  444. --- src/video/texture_manager.hpp (revision 5071)
  445. +++ src/video/texture_manager.hpp (working copy)
  446. @@ -20,7 +20,7 @@
  447. #ifndef __IMAGE_TEXTURE_MANAGER_HPP__
  448. #define __IMAGE_TEXTURE_MANAGER_HPP__
  449. -#include <GL/gl.h>
  450. +#include "glutil.hpp"
  451. #include <string>
  452. #include <vector>
  453. #include <map>
  454. Index: src/video/texture.cpp
  455. ===================================================================
  456. --- src/video/texture.cpp (revision 5071)
  457. +++ src/video/texture.cpp (working copy)
  458. @@ -20,90 +20,134 @@
  459. #include <config.h>
  460. #include "texture.hpp"
  461. +#include "gameconfig.hpp"
  462. +#include "glutil.hpp"
  463. -#include <GL/gl.h>
  464. #include <assert.h>
  465. -#include "glutil.hpp"
  466. +#include <stdexcept>
  467. static inline bool is_power_of_2(int v)
  468. {
  469. return (v & (v-1)) == 0;
  470. }
  471. +#ifdef HAVE_OPENGL
  472. Texture::Texture(unsigned int w, unsigned int h, GLenum glformat)
  473. +#else
  474. +Texture::Texture(unsigned int w, unsigned int h, GLenum)
  475. +#endif
  476. {
  477. assert(is_power_of_2(w));
  478. assert(is_power_of_2(h));
  479. +#ifdef HAVE_OPENGL
  480. + use_opengl = config->use_opengl;
  481. - this->width = w;
  482. - this->height = h;
  483. + if(use_opengl)
  484. + {
  485. + surface.opengl.width = w;
  486. + surface.opengl.height = h;
  487. - assert_gl("before creating texture");
  488. - glGenTextures(1, &handle);
  489. + assert_gl("before creating texture");
  490. + glGenTextures(1, &surface.opengl.handle);
  491. - try {
  492. - glBindTexture(GL_TEXTURE_2D, handle);
  493. + try {
  494. + glBindTexture(GL_TEXTURE_2D, surface.opengl.handle);
  495. - glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_RGBA,
  496. - GL_UNSIGNED_BYTE, 0);
  497. + glTexImage2D(GL_TEXTURE_2D, 0, glformat, surface.opengl.width,
  498. + surface.opengl.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  499. - set_texture_params();
  500. - } catch(...) {
  501. - glDeleteTextures(1, &handle);
  502. - throw;
  503. + set_texture_params();
  504. + } catch(...) {
  505. + glDeleteTextures(1, &surface.opengl.handle);
  506. + throw;
  507. + }
  508. }
  509. + else
  510. +#endif
  511. + {
  512. + surface.sdl = 0;
  513. + }
  514. }
  515. +#ifdef HAVE_OPENGL
  516. Texture::Texture(SDL_Surface* image, GLenum glformat)
  517. +#else
  518. +Texture::Texture(SDL_Surface* image, GLenum)
  519. +#endif
  520. {
  521. const SDL_PixelFormat* format = image->format;
  522. if(!is_power_of_2(image->w) || !is_power_of_2(image->h))
  523. - throw std::runtime_error("image has no power of 2 size");
  524. + throw std::runtime_error("image does not have power of 2 size");
  525. if(format->BitsPerPixel != 24 && format->BitsPerPixel != 32)
  526. - throw std::runtime_error("image has no 24 or 32 bit color depth");
  527. + throw std::runtime_error("image does not have 24 or 32 bit color depth");
  528. +#ifdef HAVE_OPENGL
  529. + use_opengl = config->use_opengl;
  530. - this->width = image->w;
  531. - this->height = image->h;
  532. + if(use_opengl)
  533. + {
  534. + surface.opengl.width = image->w;
  535. + surface.opengl.height = image->h;
  536. - assert_gl("before creating texture");
  537. - glGenTextures(1, &handle);
  538. + assert_gl("before creating texture");
  539. + glGenTextures(1, &surface.opengl.handle);
  540. - try {
  541. - GLenum sdl_format;
  542. - if(format->BytesPerPixel == 3)
  543. - sdl_format = GL_RGB;
  544. - else if(format->BytesPerPixel == 4)
  545. - sdl_format = GL_RGBA;
  546. - else
  547. - assert(false);
  548. + try {
  549. + GLenum sdl_format;
  550. + if(format->BytesPerPixel == 3)
  551. + sdl_format = GL_RGB;
  552. + else if(format->BytesPerPixel == 4)
  553. + sdl_format = GL_RGBA;
  554. + else
  555. + assert(false);
  556. - glBindTexture(GL_TEXTURE_2D, handle);
  557. - glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  558. - glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
  559. - glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, sdl_format,
  560. - GL_UNSIGNED_BYTE, image->pixels);
  561. + glBindTexture(GL_TEXTURE_2D, surface.opengl.handle);
  562. + glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  563. + glPixelStorei(GL_UNPACK_ROW_LENGTH, image->pitch/format->BytesPerPixel);
  564. + glTexImage2D(GL_TEXTURE_2D, 0, glformat, surface.opengl.width,
  565. + surface.opengl.height, 0, sdl_format,
  566. + GL_UNSIGNED_BYTE, image->pixels);
  567. - assert_gl("creating texture");
  568. + assert_gl("creating texture");
  569. - set_texture_params();
  570. - } catch(...) {
  571. - glDeleteTextures(1, &handle);
  572. - throw;
  573. + set_texture_params();
  574. + } catch(...) {
  575. + glDeleteTextures(1, &surface.opengl.handle);
  576. + throw;
  577. + }
  578. }
  579. + else
  580. +#endif
  581. + {
  582. + surface.sdl = SDL_DisplayFormatAlpha(image);
  583. + }
  584. }
  585. Texture::~Texture()
  586. {
  587. - glDeleteTextures(1, &handle);
  588. +#ifdef HAVE_OPENGL
  589. + if(use_opengl)
  590. + {
  591. + glDeleteTextures(1, &surface.opengl.handle);
  592. + }
  593. + else
  594. +#endif
  595. + {
  596. + SDL_FreeSurface(surface.sdl);
  597. + }
  598. }
  599. void
  600. Texture::set_texture_params()
  601. {
  602. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  603. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  604. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  605. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  606. +#ifdef HAVE_OPENGL
  607. + if(use_opengl)
  608. + {
  609. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  610. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  611. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  612. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  613. - assert_gl("set texture params");
  614. + assert_gl("set texture params");
  615. + }
  616. +#endif
  617. }
  618. Index: src/video/drawing_context.hpp
  619. ===================================================================
  620. --- src/video/drawing_context.hpp (revision 5071)
  621. +++ src/video/drawing_context.hpp (working copy)
  622. @@ -25,9 +25,9 @@
  623. #include <stdint.h>
  624. -#include <GL/gl.h>
  625. #include <SDL_video.h>
  626. +#include "glutil.hpp"
  627. #include "obstack/obstack.h"
  628. #include "math/vector.hpp"
  629. #include "math/rect.hpp"
  630. Index: src/video/glutil.hpp
  631. ===================================================================
  632. --- src/video/glutil.hpp (revision 5071)
  633. +++ src/video/glutil.hpp (working copy)
  634. @@ -19,9 +19,14 @@
  635. #ifndef __GLUTIL_HPP__
  636. #define __GLUTIL_HPP__
  637. +#include <config.h>
  638. +
  639. +#ifdef HAVE_OPENGL
  640. +
  641. #include <sstream>
  642. #include <stdexcept>
  643. #include <GL/gl.h>
  644. +#include <GL/glext.h>
  645. static inline void check_gl_error(const char* message)
  646. {
  647. @@ -77,4 +82,15 @@
  648. #endif
  649. }
  650. +#else
  651. +
  652. +#define GLenum int
  653. +#define GLint int
  654. +#define GL_SRC_ALPHA 0
  655. +#define GL_ONE_MINUS_SRC_ALPHA 1
  656. +#define GL_RGBA 2
  657. +#define GL_ONE 3
  658. +
  659. #endif
  660. +
  661. +#endif
  662. Index: src/video/texture.hpp
  663. ===================================================================
  664. --- src/video/texture.hpp (revision 5071)
  665. +++ src/video/texture.hpp (working copy)
  666. @@ -20,9 +20,15 @@
  667. #ifndef __TEXTURE_HPP__
  668. #define __TEXTURE_HPP__
  669. +#include <config.h>
  670. +
  671. +#include <assert.h>
  672. +
  673. #include <SDL.h>
  674. -#include <GL/gl.h>
  675. +#include "gameconfig.hpp"
  676. +#include "glutil.hpp"
  677. +
  678. /**
  679. * This class is a wrapper around a texture handle. It stores the texture width
  680. * and height and provides convenience functions for uploading SDL_Surfaces
  681. @@ -31,29 +37,82 @@
  682. class Texture
  683. {
  684. protected:
  685. - friend class TextureManager;
  686. - GLuint handle;
  687. - unsigned int width;
  688. - unsigned int height;
  689. +#ifdef HAVE_OPENGL
  690. + bool use_opengl;
  691. + union
  692. + {
  693. + struct
  694. + {
  695. + GLuint handle;
  696. + unsigned int width;
  697. + unsigned int height;
  698. + } opengl;
  699. +#else
  700. + struct
  701. + {
  702. +#endif
  703. + SDL_Surface *sdl;
  704. + } surface;
  705. public:
  706. Texture(unsigned int width, unsigned int height, GLenum glformat);
  707. - Texture(SDL_Surface* surface, GLenum glformat);
  708. + Texture(SDL_Surface* sdlsurface, GLenum glformat);
  709. virtual ~Texture();
  710. - GLuint get_handle() const
  711. +#ifdef HAVE_OPENGL
  712. + const GLuint &get_handle() const {
  713. + assert(use_opengl);
  714. + return surface.opengl.handle;
  715. + }
  716. +
  717. + void set_handle(GLuint handle) {
  718. + assert(use_opengl);
  719. + surface.opengl.handle = handle;
  720. + }
  721. +#endif
  722. +
  723. + SDL_Surface *get_surface() const
  724. {
  725. - return handle;
  726. +#ifdef HAVE_OPENGL
  727. + assert(!use_opengl);
  728. +#endif
  729. + return surface.sdl;
  730. }
  731. + void set_surface(SDL_Surface *sdlsurface)
  732. + {
  733. +#ifdef HAVE_OPENGL
  734. + assert(!use_opengl);
  735. +#endif
  736. + surface.sdl = sdlsurface;
  737. + }
  738. +
  739. unsigned int get_width() const
  740. {
  741. - return width;
  742. +#ifdef HAVE_OPENGL
  743. + if(use_opengl)
  744. + {
  745. + return surface.opengl.width;
  746. + }
  747. + else
  748. +#endif
  749. + {
  750. + return surface.sdl->w;
  751. + }
  752. }
  753. unsigned int get_height() const
  754. {
  755. - return height;
  756. +#ifdef HAVE_OPENGL
  757. + if(use_opengl)
  758. + {
  759. + return surface.opengl.height;
  760. + }
  761. + else
  762. +#endif
  763. + {
  764. + return surface.sdl->h;
  765. + }
  766. }
  767. private:
  768. Index: src/video/surface.cpp
  769. ===================================================================
  770. --- src/video/surface.cpp (revision 5071)
  771. +++ src/video/surface.cpp (working copy)
  772. @@ -29,6 +29,7 @@
  773. #include <SDL.h>
  774. #include <SDL_image.h>
  775. +#include "main.hpp"
  776. #include "gameconfig.hpp"
  777. #include "physfs/physfs_sdl.hpp"
  778. #include "video/surface.hpp"
  779. @@ -41,13 +42,32 @@
  780. {
  781. texture = texture_manager->get(file);
  782. texture->ref();
  783. - uv_left = 0;
  784. - uv_top = 0;
  785. - uv_right = texture->get_uv_right();
  786. - uv_bottom = texture->get_uv_bottom();
  787. - width = texture->get_image_width();
  788. - height = texture->get_image_height();
  789. +#ifdef HAVE_OPENGL
  790. + use_opengl = config->use_opengl;
  791. +
  792. + if(use_opengl)
  793. + {
  794. + surface.opengl.uv_left = 0;
  795. + surface.opengl.uv_top = 0;
  796. + surface.opengl.uv_right = texture->get_uv_right();
  797. + surface.opengl.uv_bottom = texture->get_uv_bottom();
  798. +
  799. + surface.opengl.width = texture->get_image_width();
  800. + surface.opengl.height = texture->get_image_height();
  801. + }
  802. + else
  803. +#endif
  804. + {
  805. + memset(transforms, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
  806. +
  807. + surface.sdl.offsetx = 0;
  808. + surface.sdl.offsety = 0;
  809. + surface.sdl.width = static_cast<int>(texture->get_image_width());
  810. + surface.sdl.height = static_cast<int>(texture->get_image_height());
  811. +
  812. + surface.sdl.flipx = false;
  813. + }
  814. }
  815. Surface::Surface(const std::string& file, int x, int y, int w, int h)
  816. @@ -55,15 +75,33 @@
  817. texture = texture_manager->get(file);
  818. texture->ref();
  819. - float tex_w = static_cast<float> (texture->get_width());
  820. - float tex_h = static_cast<float> (texture->get_height());
  821. - uv_left = static_cast<float>(x) / tex_w;
  822. - uv_top = static_cast<float>(y) / tex_h;
  823. - uv_right = static_cast<float>(x+w) / tex_w;
  824. - uv_bottom = static_cast<float>(y+h) / tex_h;
  825. +#ifdef HAVE_OPENGL
  826. + use_opengl = config->use_opengl;
  827. - width = w;
  828. - height = h;
  829. + if(use_opengl)
  830. + {
  831. + float tex_w = static_cast<float> (texture->get_width());
  832. + float tex_h = static_cast<float> (texture->get_height());
  833. + surface.opengl.uv_left = static_cast<float>(x) / tex_w;
  834. + surface.opengl.uv_top = static_cast<float>(y) / tex_h;
  835. + surface.opengl.uv_right = static_cast<float>(x+w) / tex_w;
  836. + surface.opengl.uv_bottom = static_cast<float>(y+h) / tex_h;
  837. +
  838. + surface.opengl.width = w;
  839. + surface.opengl.height = h;
  840. + }
  841. + else
  842. +#endif
  843. + {
  844. + memset(transforms, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
  845. +
  846. + surface.sdl.offsetx = x;
  847. + surface.sdl.offsety = y;
  848. + surface.sdl.width = w;
  849. + surface.sdl.height = h;
  850. +
  851. + surface.sdl.flipx = false;
  852. + }
  853. }
  854. Surface::Surface(const Surface& other)
  855. @@ -71,12 +109,30 @@
  856. texture = other.texture;
  857. texture->ref();
  858. - uv_left = other.uv_left;
  859. - uv_top = other.uv_top;
  860. - uv_right = other.uv_right;
  861. - uv_bottom = other.uv_bottom;
  862. - width = other.width;
  863. - height = other.height;
  864. +#ifdef HAVE_OPENGL
  865. + use_opengl = config->use_opengl;
  866. +
  867. + if(use_opengl)
  868. + {
  869. + surface.opengl.uv_left = other.surface.opengl.uv_left;
  870. + surface.opengl.uv_top = other.surface.opengl.uv_top;
  871. + surface.opengl.uv_right = other.surface.opengl.uv_right;
  872. + surface.opengl.uv_bottom = other.surface.opengl.uv_bottom;
  873. + surface.opengl.width = other.surface.opengl.width;
  874. + surface.opengl.height = other.surface.opengl.height;
  875. + }
  876. + else
  877. +#endif
  878. + {
  879. + memset(transforms, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
  880. +
  881. + surface.sdl.offsetx = other.surface.sdl.offsetx;
  882. + surface.sdl.offsety = other.surface.sdl.offsety;
  883. + surface.sdl.width = other.surface.sdl.width;
  884. + surface.sdl.height = other.surface.sdl.height;
  885. +
  886. + surface.sdl.flipx = other.surface.sdl.flipx;
  887. + }
  888. }
  889. const Surface&
  890. @@ -86,148 +142,584 @@
  891. texture->unref();
  892. texture = other.texture;
  893. - uv_left = other.uv_left;
  894. - uv_top = other.uv_top;
  895. - uv_right = other.uv_right;
  896. - uv_bottom = other.uv_bottom;
  897. - width = other.width;
  898. - height = other.height;
  899. +#ifdef HAVE_OPENGL
  900. + use_opengl = config->use_opengl;
  901. + if(use_opengl)
  902. + {
  903. + surface.opengl.uv_left = other.surface.opengl.uv_left;
  904. + surface.opengl.uv_top = other.surface.opengl.uv_top;
  905. + surface.opengl.uv_right = other.surface.opengl.uv_right;
  906. + surface.opengl.uv_bottom = other.surface.opengl.uv_bottom;
  907. + surface.opengl.width = other.surface.opengl.width;
  908. + surface.opengl.height = other.surface.opengl.height;
  909. + }
  910. + else
  911. +#endif
  912. + {
  913. + memset(transforms, 0, NUM_EFFECTS * sizeof(SDL_Surface *));
  914. +
  915. + surface.sdl.offsetx = other.surface.sdl.offsetx;
  916. + surface.sdl.offsety = other.surface.sdl.offsety;
  917. + surface.sdl.width = other.surface.sdl.width;
  918. + surface.sdl.height = other.surface.sdl.height;
  919. +
  920. + surface.sdl.flipx = other.surface.sdl.flipx;
  921. + }
  922. +
  923. return *this;
  924. }
  925. Surface::~Surface()
  926. {
  927. texture->unref();
  928. +
  929. +#ifdef HAVE_OPENGL
  930. + if(!use_opengl)
  931. +#endif
  932. + {
  933. + std::for_each(transforms, transforms + NUM_EFFECTS, SDL_FreeSurface);
  934. + }
  935. }
  936. void
  937. Surface::hflip()
  938. {
  939. - std::swap(uv_left, uv_right);
  940. +#ifdef HAVE_OPENGL
  941. + if(use_opengl)
  942. + {
  943. + std::swap(surface.opengl.uv_left, surface.opengl.uv_right);
  944. + }
  945. + else
  946. +#endif
  947. + {
  948. + surface.sdl.flipx = !surface.sdl.flipx;
  949. + }
  950. }
  951. -static inline void intern_draw(float left, float top, float right, float bottom, float uv_left, float uv_top,
  952. - float uv_right, float uv_bottom,
  953. - DrawingEffect effect)
  954. +#ifdef HAVE_OPENGL
  955. +namespace
  956. {
  957. - if(effect & HORIZONTAL_FLIP)
  958. - std::swap(uv_left, uv_right);
  959. - if(effect & VERTICAL_FLIP) {
  960. - std::swap(uv_top, uv_bottom);
  961. - }
  962. + inline void intern_draw(float left, float top, float right, float bottom, float uv_left, float uv_top,
  963. + float uv_right, float uv_bottom,
  964. + DrawingEffect effect)
  965. + {
  966. + if(effect & HORIZONTAL_FLIP)
  967. + std::swap(uv_left, uv_right);
  968. + if(effect & VERTICAL_FLIP) {
  969. + std::swap(uv_top, uv_bottom);
  970. + }
  971. - glBegin(GL_QUADS);
  972. - glTexCoord2f(uv_left, uv_top);
  973. - glVertex2f(left, top);
  974. + glBegin(GL_QUADS);
  975. + glTexCoord2f(uv_left, uv_top);
  976. + glVertex2f(left, top);
  977. - glTexCoord2f(uv_right, uv_top);
  978. - glVertex2f(right, top);
  979. + glTexCoord2f(uv_right, uv_top);
  980. + glVertex2f(right, top);
  981. - glTexCoord2f(uv_right, uv_bottom);
  982. - glVertex2f(right, bottom);
  983. + glTexCoord2f(uv_right, uv_bottom);
  984. + glVertex2f(right, bottom);
  985. - glTexCoord2f(uv_left, uv_bottom);
  986. - glVertex2f(left, bottom);
  987. - glEnd();
  988. -}
  989. -
  990. -static inline void intern_draw2(float left, float top, float right, float bottom,
  991. - float uv_left, float uv_top,
  992. - float uv_right, float uv_bottom,
  993. - float angle, float alpha,
  994. - const Color& color,
  995. - const Blend& blend,
  996. - DrawingEffect effect)
  997. -{
  998. - if(effect & HORIZONTAL_FLIP)
  999. - std::swap(uv_left, uv_right);
  1000. - if(effect & VERTICAL_FLIP) {
  1001. - std::swap(uv_top, uv_bottom);
  1002. + glTexCoord2f(uv_left, uv_bottom);
  1003. + glVertex2f(left, bottom);
  1004. + glEnd();
  1005. }
  1006. - float center_x = (left + right) / 2;
  1007. - float center_y = (top + bottom) / 2;
  1008. + inline void intern_draw2(float left, float top, float right, float bottom,
  1009. + float uv_left, float uv_top,
  1010. + float uv_right, float uv_bottom,
  1011. + float angle, float alpha,
  1012. + const Color& color,
  1013. + const Blend& blend,
  1014. + DrawingEffect effect)
  1015. + {
  1016. + if(effect & HORIZONTAL_FLIP)
  1017. + std::swap(uv_left, uv_right);
  1018. + if(effect & VERTICAL_FLIP) {
  1019. + std::swap(uv_top, uv_bottom);
  1020. + }
  1021. - float sa = sinf(angle/180.0f*M_PI);
  1022. - float ca = cosf(angle/180.0f*M_PI);
  1023. + float center_x = (left + right) / 2;
  1024. + float center_y = (top + bottom) / 2;
  1025. - left -= center_x;
  1026. - right -= center_x;
  1027. + float sa = sinf(angle/180.0f*M_PI);
  1028. + float ca = cosf(angle/180.0f*M_PI);
  1029. - top -= center_y;
  1030. - bottom -= center_y;
  1031. + left -= center_x;
  1032. + right -= center_x;
  1033. - glBlendFunc(blend.sfactor, blend.dfactor);
  1034. - glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
  1035. - glBegin(GL_QUADS);
  1036. - glTexCoord2f(uv_left, uv_top);
  1037. - glVertex2f(left*ca - top*sa + center_x,
  1038. - left*sa + top*ca + center_y);
  1039. + top -= center_y;
  1040. + bottom -= center_y;
  1041. - glTexCoord2f(uv_right, uv_top);
  1042. - glVertex2f(right*ca - top*sa + center_x,
  1043. - right*sa + top*ca + center_y);
  1044. + glBlendFunc(blend.sfactor, blend.dfactor);
  1045. + glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
  1046. + glBegin(GL_QUADS);
  1047. + glTexCoord2f(uv_left, uv_top);
  1048. + glVertex2f(left*ca - top*sa + center_x,
  1049. + left*sa + top*ca + center_y);
  1050. - glTexCoord2f(uv_right, uv_bottom);
  1051. - glVertex2f(right*ca - bottom*sa + center_x,
  1052. - right*sa + bottom*ca + center_y);
  1053. + glTexCoord2f(uv_right, uv_top);
  1054. + glVertex2f(right*ca - top*sa + center_x,
  1055. + right*sa + top*ca + center_y);
  1056. - glTexCoord2f(uv_left, uv_bottom);
  1057. - glVertex2f(left*ca - bottom*sa + center_x,
  1058. - left*sa + bottom*ca + center_y);
  1059. - glEnd();
  1060. + glTexCoord2f(uv_right, uv_bottom);
  1061. + glVertex2f(right*ca - bottom*sa + center_x,
  1062. + right*sa + bottom*ca + center_y);
  1063. - // FIXME: find a better way to restore the blend mode
  1064. - glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  1065. - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1066. + glTexCoord2f(uv_left, uv_bottom);
  1067. + glVertex2f(left*ca - bottom*sa + center_x,
  1068. + left*sa + bottom*ca + center_y);
  1069. + glEnd();
  1070. +
  1071. + // FIXME: find a better way to restore the blend mode
  1072. + glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
  1073. + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1074. + }
  1075. }
  1076. +#endif
  1077. void
  1078. -Surface::draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const
  1079. +Surface::draw(float x, float y, float alpha,
  1080. +#ifdef HAVE_OPENGL
  1081. + float angle, const Color& color, const Blend& blend,
  1082. +#else
  1083. + float, const Color&, const Blend&,
  1084. +#endif
  1085. + DrawingEffect effect) const
  1086. {
  1087. - glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1088. -
  1089. - intern_draw2(x, y,
  1090. - x + width, y + height,
  1091. - uv_left, uv_top, uv_right, uv_bottom,
  1092. - angle,
  1093. - alpha,
  1094. - color,
  1095. - blend,
  1096. - effect);
  1097. +#ifdef HAVE_OPENGL
  1098. + if(use_opengl)
  1099. + {
  1100. + glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1101. + intern_draw2(x, y,
  1102. + x + surface.opengl.width, y + surface.opengl.height,
  1103. + surface.opengl.uv_left, surface.opengl.uv_top,
  1104. + surface.opengl.uv_right, surface.opengl.uv_bottom,
  1105. + angle,
  1106. + alpha,
  1107. + color,
  1108. + blend,
  1109. + effect);
  1110. + }
  1111. + else
  1112. +#endif
  1113. + {
  1114. + draw_part(0, 0, x, y, surface.sdl.width, surface.sdl.height, alpha, effect);
  1115. + }
  1116. }
  1117. void
  1118. Surface::draw(float x, float y, float alpha, DrawingEffect effect) const
  1119. {
  1120. - glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1121. +#ifdef HAVE_OPENGL
  1122. + if(use_opengl)
  1123. + {
  1124. + glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1125. + glColor4f(1, 1, 1, alpha);
  1126. + intern_draw(x, y,
  1127. + x + surface.opengl.width, y + surface.opengl.height,
  1128. + surface.opengl.uv_left, surface.opengl.uv_top,
  1129. + surface.opengl.uv_right, surface.opengl.uv_bottom, effect);
  1130. + glColor4f(1, 1, 1, 1);
  1131. + }
  1132. + else
  1133. +#endif
  1134. + {
  1135. + draw_part(0, 0, x, y, surface.sdl.width, surface.sdl.height, alpha, effect);
  1136. + }
  1137. +}
  1138. - glColor4f(1, 1, 1, alpha);
  1139. - intern_draw(x, y,
  1140. - x + width, y + height,
  1141. - uv_left, uv_top, uv_right, uv_bottom, effect);
  1142. - glColor4f(1, 1, 1, 1);
  1143. +namespace
  1144. +{
  1145. +#define BILINEAR
  1146. +
  1147. +#ifdef NAIVE
  1148. + SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator)
  1149. + {
  1150. + if(numerator == denominator)
  1151. + {
  1152. + src->refcount++;
  1153. + return src;
  1154. + }
  1155. + else
  1156. + {
  1157. + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w * numerator / denominator, src->h * numerator / denominator, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
  1158. + int bpp = dst->format->BytesPerPixel;
  1159. + for(int y = 0;y < dst->h;y++) {
  1160. + for(int x = 0;x < dst->w;x++) {
  1161. + Uint8 *srcpixel = (Uint8 *) src->pixels + (y * denominator / numerator) * src->pitch + (x * denominator / numerator) * bpp;
  1162. + Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
  1163. + switch(bpp) {
  1164. + case 4:
  1165. + dstpixel[3] = srcpixel[3];
  1166. + case 3:
  1167. + dstpixel[2] = srcpixel[2];
  1168. + case 2:
  1169. + dstpixel[1] = srcpixel[1];
  1170. + case 1:
  1171. + dstpixel[0] = srcpixel[0];
  1172. + }
  1173. + }
  1174. + }
  1175. + return dst;
  1176. + }
  1177. + }
  1178. +#endif
  1179. +
  1180. +#ifdef BILINEAR
  1181. + void getpixel(SDL_Surface *src, int srcx, int srcy, Uint8 color[4])
  1182. + {
  1183. + int bpp = src->format->BytesPerPixel;
  1184. + Uint8 *srcpixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
  1185. + Uint32 mapped = 0;
  1186. + switch(bpp) {
  1187. + case 1:
  1188. + mapped = *srcpixel;
  1189. + break;
  1190. + case 2:
  1191. + mapped = *(Uint16 *)srcpixel;
  1192. + break;
  1193. + case 3:
  1194. +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1195. + mapped |= srcpixel[0] << 16;
  1196. + mapped |= srcpixel[1] << 8;
  1197. + mapped |= srcpixel[2] << 0;
  1198. +#else
  1199. + mapped |= srcpixel[0] << 0;
  1200. + mapped |= srcpixel[1] << 8;
  1201. + mapped |= srcpixel[2] << 16;
  1202. +#endif
  1203. + break;
  1204. + case 4:
  1205. + mapped = *(Uint32 *)srcpixel;
  1206. + break;
  1207. + }
  1208. + SDL_GetRGBA(mapped, src->format, &color[0], &color[1], &color[2], &color[3]);
  1209. + }
  1210. +
  1211. + void merge(Uint8 color[4], Uint8 color0[4], Uint8 color1[4], int rem, int total)
  1212. + {
  1213. + color[0] = (color0[0] * (total - rem) + color1[0] * rem) / total;
  1214. + color[1] = (color0[1] * (total - rem) + color1[1] * rem) / total;
  1215. + color[2] = (color0[2] * (total - rem) + color1[2] * rem) / total;
  1216. + color[3] = (color0[3] * (total - rem) + color1[3] * rem) / total;
  1217. + }
  1218. +
  1219. + SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator)
  1220. + {
  1221. + if(numerator == denominator)
  1222. + {
  1223. + src->refcount++;
  1224. + return src;
  1225. + }
  1226. + else
  1227. + {
  1228. + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w * numerator / denominator, src->h * numerator / denominator, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
  1229. + int bpp = dst->format->BytesPerPixel;
  1230. + for(int y = 0;y < dst->h;y++) {
  1231. + for(int x = 0;x < dst->w;x++) {
  1232. + int srcx = x * denominator / numerator;
  1233. + int srcy = y * denominator / numerator;
  1234. + Uint8 color00[4], color01[4], color10[4], color11[4];
  1235. + getpixel(src, srcx, srcy, color00);
  1236. + getpixel(src, srcx + 1, srcy, color01);
  1237. + getpixel(src, srcx, srcy + 1, color10);
  1238. + getpixel(src, srcx + 1, srcy + 1, color11);
  1239. + Uint8 color0[4], color1[4], color[4];
  1240. + int remx = x * denominator % numerator;
  1241. + merge(color0, color00, color01, remx, numerator);
  1242. + merge(color1, color10, color11, remx, numerator);
  1243. + int remy = y * denominator % numerator;
  1244. + merge(color, color0, color1, remy, numerator);
  1245. + Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
  1246. + Uint32 mapped = SDL_MapRGBA(dst->format, color[0], color[1], color[2], color[3]);
  1247. + switch(bpp) {
  1248. + case 1:
  1249. + *dstpixel = mapped;
  1250. + break;
  1251. + case 2:
  1252. + *(Uint16 *)dstpixel = mapped;
  1253. + break;
  1254. + case 3:
  1255. +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1256. + dstpixel[0] = (mapped >> 16) & 0xff;
  1257. + dstpixel[1] = (mapped >> 8) & 0xff;
  1258. + dstpixel[2] = (mapped >> 0) & 0xff;
  1259. +#else
  1260. + dstpixel[0] = (mapped >> 0) & 0xff;
  1261. + dstpixel[1] = (mapped >> 8) & 0xff;
  1262. + dstpixel[2] = (mapped >> 16) & 0xff;
  1263. +#endif
  1264. + break;
  1265. + case 4:
  1266. + *(Uint32 *)dstpixel = mapped;
  1267. + break;
  1268. + }
  1269. + }
  1270. + }
  1271. + return dst;
  1272. + }
  1273. + }
  1274. +#endif
  1275. +
  1276. + // FIXME: Horizontal and vertical line problem
  1277. +#ifdef BRESENHAM
  1278. + void accumulate(SDL_Surface *src, int srcx, int srcy, int color[4], int weight)
  1279. + {
  1280. + if(srcx < 0 || srcy < 0 || weight == 0) {
  1281. + return;
  1282. + }
  1283. + int bpp = src->format->BytesPerPixel;
  1284. + Uint8 *srcpixel = (Uint8 *) src->pixels + srcy * src->pitch + srcx * bpp;
  1285. + Uint32 mapped = 0;
  1286. + switch(bpp) {
  1287. + case 1:
  1288. + mapped = *srcpixel;
  1289. + break;
  1290. + case 2:
  1291. + mapped = *(Uint16 *)srcpixel;
  1292. + break;
  1293. + case 3:
  1294. +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1295. + mapped |= srcpixel[0] << 16;
  1296. + mapped |= srcpixel[1] << 8;
  1297. + mapped |= srcpixel[2] << 0;
  1298. +#else
  1299. + mapped |= srcpixel[0] << 0;
  1300. + mapped |= srcpixel[1] << 8;
  1301. + mapped |= srcpixel[2] << 16;
  1302. +#endif
  1303. + break;
  1304. + case 4:
  1305. + mapped = *(Uint32 *)srcpixel;
  1306. + break;
  1307. + }
  1308. + Uint8 red, green, blue, alpha;
  1309. + SDL_GetRGBA(mapped, src->format, &red, &green, &blue, &alpha);
  1310. + color[0] += red * weight;
  1311. + color[1] += green * weight;
  1312. + color[2] += blue * weight;
  1313. + color[3] += alpha * weight;
  1314. + }
  1315. +
  1316. + void accumulate_line(SDL_Surface *src, int srcy, int line[][4], int linesize, int weight, int numerator, int denominator)
  1317. + {
  1318. + int intpart = denominator / numerator;
  1319. + int fractpart = denominator % numerator;
  1320. + for(int x = 0, xe = 0, srcx = 0;x < linesize;x++) {
  1321. + accumulate(src, srcx, srcy, line[x], (numerator - xe) * weight);
  1322. + srcx++;
  1323. + for(int i = 0;i < intpart - 1;i++) {
  1324. + accumulate(src, srcx, srcy, line[x], numerator * weight);
  1325. + srcx++;
  1326. + }
  1327. + xe += fractpart;
  1328. + if(xe >= numerator) {
  1329. + xe -= numerator;
  1330. + srcx++;
  1331. + }
  1332. + accumulate(src, srcx, srcy, line[x], xe * weight);
  1333. + }
  1334. + }
  1335. +
  1336. + SDL_Surface *scale(SDL_Surface *src, int numerator, int denominator)
  1337. + {
  1338. + if(numerator == denominator)
  1339. + {
  1340. + src->refcount++;
  1341. + return src;
  1342. + }
  1343. + else
  1344. + {
  1345. + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w * numerator / denominator, src->h * numerator / denominator, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
  1346. + int bpp = dst->format->BytesPerPixel;
  1347. + int intpart = denominator / numerator;
  1348. + int fractpart = denominator % numerator;
  1349. + for(int y = 0, ye = 0, srcy = 0;y < dst->h;y++) {
  1350. + int line[dst->w][4];
  1351. + memset(line, 0, sizeof(int) * dst->w * 4);
  1352. + accumulate_line(src, srcy, line, dst->w, numerator - ye, numerator, denominator);
  1353. + srcy++;
  1354. + for(int i = 0;i < intpart - 1;i++) {
  1355. + accumulate_line(src, srcy, line, dst->w, numerator, numerator, denominator);
  1356. + srcy++;
  1357. + }
  1358. + ye += fractpart;
  1359. + if(ye >= numerator) {
  1360. + ye -= numerator;
  1361. + srcy++;
  1362. + }
  1363. + accumulate_line(src, srcy, line, dst->w, ye, numerator, denominator);
  1364. + for(int x = 0;x < dst->w;x++) {
  1365. + Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
  1366. + Uint32 mapped = SDL_MapRGBA(dst->format, line[x][0] / (denominator * denominator), line[x][1] / (denominator * denominator), line[x][2] / (denominator * denominator), line[x][3] / (denominator * denominator));
  1367. + switch(bpp) {
  1368. + case 1:
  1369. + *dstpixel = mapped;
  1370. + break;
  1371. + case 2:
  1372. + *(Uint16 *)dstpixel = mapped;
  1373. + break;
  1374. + case 3:
  1375. +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1376. + dstpixel[0] = (mapped >> 16) & 0xff;
  1377. + dstpixel[1] = (mapped >> 8) & 0xff;
  1378. + dstpixel[2] = (mapped >> 0) & 0xff;
  1379. +#else
  1380. + dstpixel[0] = (mapped >> 0) & 0xff;
  1381. + dstpixel[1] = (mapped >> 8) & 0xff;
  1382. + dstpixel[2] = (mapped >> 16) & 0xff;
  1383. +#endif
  1384. + break;
  1385. + case 4:
  1386. + *(Uint32 *)dstpixel = mapped;
  1387. + break;
  1388. + }
  1389. + }
  1390. + }
  1391. + return dst;
  1392. + }
  1393. + }
  1394. +#endif
  1395. +
  1396. + SDL_Surface *horz_flip(SDL_Surface *src)
  1397. + {
  1398. + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
  1399. + int bpp = dst->format->BytesPerPixel;
  1400. + for(int y = 0;y < dst->h;y++) {
  1401. + for(int x = 0;x < dst->w;x++) {
  1402. + Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
  1403. + Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + (dst->w - x - 1) * bpp;
  1404. + switch(bpp) {
  1405. + case 4:
  1406. + dstpixel[3] = srcpixel[3];
  1407. + case 3:
  1408. + dstpixel[2] = srcpixel[2];
  1409. + case 2:
  1410. + dstpixel[1] = srcpixel[1];
  1411. + case 1:
  1412. + dstpixel[0] = srcpixel[0];
  1413. + }
  1414. + }
  1415. + }
  1416. + return dst;
  1417. + }
  1418. +
  1419. + SDL_Surface *vert_flip(SDL_Surface *src)
  1420. + {
  1421. + SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask);
  1422. + int bpp = dst->format->BytesPerPixel;
  1423. + for(int y = 0;y < dst->h;y++) {
  1424. + for(int x = 0;x < dst->w;x++) {
  1425. + Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
  1426. + Uint8 *dstpixel = (Uint8 *) dst->pixels + (dst->h - y - 1) * dst->pitch + x * bpp;
  1427. + switch(bpp) {
  1428. + case 4:
  1429. + dstpixel[3] = srcpixel[3];
  1430. + case 3:
  1431. + dstpixel[2] = srcpixel[2];
  1432. + case 2:
  1433. + dstpixel[1] = srcpixel[1];
  1434. + case 1:
  1435. + dstpixel[0] = srcpixel[0];
  1436. + }
  1437. + }
  1438. + }
  1439. + return dst;
  1440. + }
  1441. }
  1442. void
  1443. Surface::draw_part(float src_x, float src_y, float dst_x, float dst_y,
  1444. +#ifdef HAVE_OPENGL
  1445. float width, float height, float alpha,
  1446. +#else
  1447. + float width, float height, float,
  1448. +#endif
  1449. DrawingEffect effect) const
  1450. {
  1451. - float uv_width = uv_right - uv_left;
  1452. - float uv_height = uv_bottom - uv_top;
  1453. +#ifdef HAVE_OPENGL
  1454. + if(use_opengl)
  1455. + {
  1456. + float uv_width = surface.opengl.uv_right - surface.opengl.uv_left;
  1457. + float uv_height = surface.opengl.uv_bottom - surface.opengl.uv_top;
  1458. - float uv_left = this->uv_left + (uv_width * src_x) / this->width;
  1459. - float uv_top = this->uv_top + (uv_height * src_y) / this->height;
  1460. - float uv_right = this->uv_left + (uv_width * (src_x + width)) / this->width;
  1461. - float uv_bottom = this->uv_top + (uv_height * (src_y + height)) / this->height;
  1462. + float uv_left = surface.opengl.uv_left + (uv_width * src_x) / surface.opengl.width;
  1463. + float uv_top = surface.opengl.uv_top + (uv_height * src_y) / surface.opengl.height;
  1464. + float uv_right = surface.opengl.uv_left + (uv_width * (src_x + width)) / surface.opengl.width;
  1465. + float uv_bottom = surface.opengl.uv_top + (uv_height * (src_y + height)) / surface.opengl.height;
  1466. - glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1467. + glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1468. + glColor4f(1, 1, 1, alpha);
  1469. + intern_draw(dst_x, dst_y,
  1470. + dst_x + width, dst_y + height,
  1471. + uv_left, uv_top, uv_right, uv_bottom, effect);
  1472. + glColor4f(1, 1, 1, 1);
  1473. + }
  1474. + else
  1475. +#endif
  1476. + {
  1477. + //FIXME: support parameter "alpha"
  1478. +
  1479. + // get and check SDL_Surface
  1480. + if (texture->get_surface() == 0) {
  1481. + std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
  1482. + return;
  1483. + }
  1484. - glColor4f(1, 1, 1, alpha);
  1485. - intern_draw(dst_x, dst_y,
  1486. - dst_x + width, dst_y + height,
  1487. - uv_left, uv_top, uv_right, uv_bottom, effect);
  1488. - glColor4f(1, 1, 1, 1);
  1489. + if (surface.sdl.flipx) effect = HORIZONTAL_FLIP;
  1490. +
  1491. + float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
  1492. + float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
  1493. + int numerator;
  1494. + int denominator;
  1495. + if(xfactor < yfactor)
  1496. + {
  1497. + numerator = config->screenwidth;
  1498. + denominator = SCREEN_WIDTH;
  1499. + }
  1500. + else
  1501. + {
  1502. + numerator = config->screenheight;
  1503. + denominator = SCREEN_HEIGHT;
  1504. + }
  1505. +
  1506. + if(transforms[effect] == 0) {
  1507. + if(transforms[NO_EFFECT] == 0) {
  1508. + transforms[NO_EFFECT] = scale(texture->get_surface(), numerator, denominator);
  1509. + }
  1510. + switch(effect) {
  1511. + case NO_EFFECT:
  1512. + break;
  1513. + case HORIZONTAL_FLIP:
  1514. + transforms[HORIZONTAL_FLIP] = horz_flip(transforms[NO_EFFECT]);
  1515. + break;
  1516. + case VERTICAL_FLIP:
  1517. + transforms[VERTICAL_FLIP] = vert_flip(transforms[NO_EFFECT]);
  1518. + break;
  1519. + default:
  1520. + std::cerr << "Warning: No known transformation applies to surface, skipped draw" << std::endl;
  1521. + return;
  1522. + }
  1523. + }
  1524. +
  1525. + int ox = surface.sdl.offsetx;
  1526. + if (effect == HORIZONTAL_FLIP) ox = static_cast<int>(texture->get_surface()->w) - (ox+static_cast<int>(width));
  1527. + int oy = surface.sdl.offsety;
  1528. + if (effect == VERTICAL_FLIP) oy = static_cast<int>(texture->get_surface()->h) - (oy+static_cast<int>(height));
  1529. + // draw surface to screen
  1530. + SDL_Surface* screen = SDL_GetVideoSurface();
  1531. +
  1532. + SDL_Rect srcRect;
  1533. + srcRect.x = static_cast<int>((ox + src_x) * numerator / denominator);
  1534. + srcRect.y = static_cast<int>((oy + src_y) * numerator / denominator);
  1535. + srcRect.w = static_cast<int>(width * numerator / denominator);
  1536. + srcRect.h = static_cast<int>(height * numerator / denominator);
  1537. +
  1538. + SDL_Rect dstRect;
  1539. + dstRect.x = static_cast<int>(dst_x * numerator / denominator);
  1540. + dstRect.y = static_cast<int>(dst_y * numerator / denominator);
  1541. +
  1542. + SDL_BlitSurface(transforms[effect], &srcRect, screen, &dstRect);
  1543. + }
  1544. }
  1545. Index: src/video/texture_manager.cpp
  1546. ===================================================================
  1547. --- src/video/texture_manager.cpp (revision 5071)
  1548. +++ src/video/texture_manager.cpp (working copy)
  1549. @@ -24,14 +24,13 @@
  1550. #include <assert.h>
  1551. #include <SDL.h>
  1552. #include <SDL_image.h>
  1553. -#include <GL/gl.h>
  1554. -#include <GL/glext.h>
  1555. #include <iostream>
  1556. #include <sstream>
  1557. #include <stdexcept>
  1558. #include "physfs/physfs_sdl.hpp"
  1559. #include "image_texture.hpp"
  1560. #include "glutil.hpp"
  1561. +#include "gameconfig.hpp"
  1562. #include "file_system.hpp"
  1563. #include "log.hpp"
  1564. @@ -149,12 +148,16 @@
  1565. void
  1566. TextureManager::save_textures()
  1567. {
  1568. - glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  1569. - glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
  1570. - glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  1571. - glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  1572. - glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
  1573. - glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1574. +#ifdef HAVE_OPENGL
  1575. + if(config->use_opengl) {
  1576. + glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  1577. + glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
  1578. + glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
  1579. + glPixelStorei(GL_PACK_SKIP_ROWS, 0);
  1580. + glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
  1581. + glPixelStorei(GL_PACK_ALIGNMENT, 1);
  1582. + }
  1583. +#endif
  1584. for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
  1585. save_texture(*i);
  1586. }
  1587. @@ -169,74 +172,90 @@
  1588. {
  1589. SavedTexture saved_texture;
  1590. saved_texture.texture = texture;
  1591. - glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1592. - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
  1593. - &saved_texture.width);
  1594. - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
  1595. - &saved_texture.height);
  1596. - glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
  1597. - &saved_texture.border);
  1598. - glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  1599. - &saved_texture.min_filter);
  1600. - glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  1601. - &saved_texture.mag_filter);
  1602. - glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  1603. - &saved_texture.wrap_s);
  1604. - glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  1605. - &saved_texture.wrap_t);
  1606. +#ifdef HAVE_OPENGL
  1607. + if(config->use_opengl) {
  1608. + glBindTexture(GL_TEXTURE_2D, texture->get_handle());
  1609. + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
  1610. + &saved_texture.width);
  1611. + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
  1612. + &saved_texture.height);
  1613. + glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_BORDER,
  1614. + &saved_texture.border);
  1615. + glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  1616. + &saved_texture.min_filter);
  1617. + glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  1618. + &saved_texture.mag_filter);
  1619. + glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  1620. + &saved_texture.wrap_s);
  1621. + glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  1622. + &saved_texture.wrap_t);
  1623. + }
  1624. +#endif
  1625. size_t pixelssize = saved_texture.width * saved_texture.height * 4;
  1626. saved_texture.pixels = new char[pixelssize];
  1627. - glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  1628. - saved_texture.pixels);
  1629. +#ifdef HAVE_OPENGL
  1630. + if(config->use_opengl) {
  1631. + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
  1632. + saved_texture.pixels);
  1633. + }
  1634. +#endif
  1635. saved_textures.push_back(saved_texture);
  1636. - glDeleteTextures(1, &(texture->handle));
  1637. - texture->handle = 0;
  1638. +#ifdef HAVE_OPENGL
  1639. + if(config->use_opengl) {
  1640. + glDeleteTextures(1, &(texture->get_handle()));
  1641. + texture->set_handle(0);
  1642. - assert_gl("retrieving texture for save");
  1643. + assert_gl("retrieving texture for save");
  1644. + }
  1645. +#endif
  1646. }
  1647. void
  1648. TextureManager::reload_textures()
  1649. {
  1650. - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  1651. - glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
  1652. - glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  1653. - glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  1654. - glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
  1655. - glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1656. +#ifdef HAVE_OPENGL
  1657. + if(config->use_opengl) {
  1658. + glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  1659. + glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
  1660. + glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  1661. + glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  1662. + glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
  1663. + glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  1664. - for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
  1665. - i != saved_textures.end(); ++i) {
  1666. - SavedTexture& saved_texture = *i;
  1667. + for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
  1668. + i != saved_textures.end(); ++i) {
  1669. + SavedTexture& saved_texture = *i;
  1670. - GLuint handle;
  1671. - glGenTextures(1, &handle);
  1672. - assert_gl("creating texture handle");
  1673. + GLuint handle;
  1674. + glGenTextures(1, &handle);
  1675. + assert_gl("creating texture handle");
  1676. - glBindTexture(GL_TEXTURE_2D, handle);
  1677. - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
  1678. - saved_texture.width, saved_texture.height,
  1679. - saved_texture.border, GL_RGBA,
  1680. - GL_UNSIGNED_BYTE, saved_texture.pixels);
  1681. - delete[] saved_texture.pixels;
  1682. - assert_gl("uploading texture pixel data");
  1683. + glBindTexture(GL_TEXTURE_2D, handle);
  1684. + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
  1685. + saved_texture.width, saved_texture.height,
  1686. + saved_texture.border, GL_RGBA,
  1687. + GL_UNSIGNED_BYTE, saved_texture.pixels);
  1688. + delete[] saved_texture.pixels;
  1689. + assert_gl("uploading texture pixel data");
  1690. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  1691. - saved_texture.min_filter);
  1692. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  1693. - saved_texture.mag_filter);
  1694. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  1695. - saved_texture.wrap_s);
  1696. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  1697. - saved_texture.wrap_t);
  1698. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  1699. + saved_texture.min_filter);
  1700. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  1701. + saved_texture.mag_filter);
  1702. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  1703. + saved_texture.wrap_s);
  1704. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  1705. + saved_texture.wrap_t);
  1706. - assert_gl("setting texture_params");
  1707. - saved_texture.texture->handle = handle;
  1708. + assert_gl("setting texture_params");
  1709. + saved_texture.texture->set_handle(handle);
  1710. + }
  1711. }
  1712. +#endif
  1713. saved_textures.clear();
  1714. }
  1715. Index: src/video/surface.hpp
  1716. ===================================================================
  1717. --- src/video/surface.hpp (revision 5071)
  1718. +++ src/video/surface.hpp (working copy)
  1719. @@ -20,7 +20,11 @@
  1720. #ifndef __SURFACE_HPP__
  1721. #define __SURFACE_HPP__
  1722. +#include <config.h>
  1723. +
  1724. #include <string>
  1725. +#include <SDL.h>
  1726. +#include "gameconfig.hpp"
  1727. #include "math/vector.hpp"
  1728. class Color;
  1729. @@ -30,11 +34,12 @@
  1730. /// bitset for drawing effects
  1731. enum DrawingEffect {
  1732. /** Don't apply anything */
  1733. - NO_EFFECT = 0x0000,
  1734. + NO_EFFECT,
  1735. /** Draw the Surface upside down */
  1736. - VERTICAL_FLIP = 0x0001,
  1737. + VERTICAL_FLIP,
  1738. /** Draw the Surface from left to down */
  1739. - HORIZONTAL_FLIP = 0x0002,
  1740. + HORIZONTAL_FLIP,
  1741. + NUM_EFFECTS
  1742. };
  1743. /**
  1744. @@ -47,21 +52,43 @@
  1745. private:
  1746. friend class DrawingContext;
  1747. friend class Font;
  1748. - ImageTexture* texture;
  1749. - float uv_left;
  1750. - float uv_top;
  1751. - float uv_right;
  1752. - float uv_bottom;
  1753. -
  1754. void draw(float x, float y, float alpha, float angle, const Color& color, const Blend& blend, DrawingEffect effect) const;
  1755. void draw(float x, float y, float alpha, DrawingEffect effect) const;
  1756. void draw_part(float src_x, float src_y, float dst_x, float dst_y,
  1757. float width, float height,
  1758. float alpha, DrawingEffect effect) const;
  1759. - float width;
  1760. - float height;
  1761. + ImageTexture* texture;
  1762. +#ifdef HAVE_OPENGL
  1763. + bool use_opengl;
  1764. + union
  1765. + {
  1766. + struct
  1767. + {
  1768. + float uv_left;
  1769. + float uv_top;
  1770. + float uv_right;
  1771. + float uv_bottom;
  1772. +
  1773. + float width;
  1774. + float height;
  1775. + } opengl;
  1776. +#else
  1777. + struct
  1778. + {
  1779. +#endif
  1780. + struct
  1781. + {
  1782. + bool flipx;
  1783. + int offsetx; /**< Region in ::surface to be used for blitting */
  1784. + int offsety; /**< Region in ::surface to be used for blitting */
  1785. + int width; /**< Region in ::surface to be used for blitting */
  1786. + int height; /**< Region in ::surface to be used for blitting */
  1787. + } sdl;
  1788. + } surface;
  1789. + mutable SDL_Surface *transforms[NUM_EFFECTS]; /**< Cache for pre-transformed surfaces */
  1790. +
  1791. public:
  1792. Surface(const std::string& file);
  1793. Surface(const std::string& file, int x, int y, int w, int h);
  1794. @@ -75,12 +102,30 @@
  1795. float get_width() const
  1796. {
  1797. - return width;
  1798. +#ifdef HAVE_OPENGL
  1799. + if(use_opengl)
  1800. + {
  1801. + return surface.opengl.width;
  1802. + }
  1803. + else
  1804. +#endif
  1805. + {
  1806. + return surface.sdl.width;
  1807. + }
  1808. }
  1809. float get_height() const
  1810. {
  1811. - return height;
  1812. +#ifdef HAVE_OPENGL
  1813. + if(use_opengl)
  1814. + {
  1815. + return surface.opengl.height;
  1816. + }
  1817. + else
  1818. +#endif
  1819. + {
  1820. + return surface.sdl.height;
  1821. + }
  1822. }
  1823. /**
  1824. Index: src/gameconfig.cpp
  1825. ===================================================================
  1826. --- src/gameconfig.cpp (revision 5071)
  1827. +++ src/gameconfig.cpp (working copy)
  1828. @@ -36,6 +36,9 @@
  1829. Config::Config()
  1830. {
  1831. use_fullscreen = true;
  1832. +#ifdef HAVE_OPENGL
  1833. + use_opengl = true;
  1834. +#endif
  1835. try_vsync = true;
  1836. show_fps = false;
  1837. sound_enabled = true;
  1838. @@ -70,7 +73,10 @@
  1839. const lisp::Lisp* config_video_lisp = config_lisp->get_lisp("video");
  1840. if(config_video_lisp) {
  1841. config_video_lisp->get("fullscreen", use_fullscreen);
  1842. - config_video_lisp->get("vsync", try_vsync);
  1843. +#ifdef HAVE_OPENGL
  1844. + config_video_lisp->get("opengl", use_opengl);
  1845. +#endif
  1846. + config_video_lisp->get("vsync", try_vsync);
  1847. config_video_lisp->get("width", screenwidth);
  1848. config_video_lisp->get("height", screenheight);
  1849. config_video_lisp->get("aspect_ratio", aspect_ratio);
  1850. @@ -100,6 +106,9 @@
  1851. writer.start_list("video");
  1852. writer.write_bool("fullscreen", use_fullscreen);
  1853. +#ifdef HAVE_OPENGL
  1854. + writer.write_bool("opengl", use_opengl);
  1855. +#endif
  1856. writer.write_bool("vsync", try_vsync);
  1857. writer.write_int("width", screenwidth);
  1858. writer.write_int("height", screenheight);
  1859. Index: src/main.cpp
  1860. ===================================================================
  1861. --- src/main.cpp (revision 5071)
  1862. +++ src/main.cpp (working copy)
  1863. @@ -33,7 +33,6 @@
  1864. #include <physfs.h>
  1865. #include <SDL.h>
  1866. #include <SDL_image.h>
  1867. -#include <GL/gl.h>
  1868. #include "gameconfig.hpp"
  1869. #include "resources.hpp"
  1870. @@ -383,7 +382,11 @@
  1871. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  1872. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  1873. - int flags = SDL_OPENGL;
  1874. +#ifdef HAVE_OPENGL
  1875. + int flags = config->use_opengl ? SDL_OPENGL : SDL_SWSURFACE;
  1876. +#else
  1877. + int flags = SDL_SWSURFACE;
  1878. +#endif
  1879. if(config->use_fullscreen)
  1880. flags |= SDL_FULLSCREEN;
  1881. int width = config->screenwidth;
  1882. @@ -437,23 +440,28 @@
  1883. log_info << (config->use_fullscreen?"fullscreen ":"window ") << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << " Ratio: " << aspect_ratio << "\n";
  1884. - // setup opengl state and transform
  1885. - glDisable(GL_DEPTH_TEST);
  1886. - glDisable(GL_CULL_FACE);
  1887. - glEnable(GL_TEXTURE_2D);
  1888. - glEnable(GL_BLEND);
  1889. - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1890. +#ifdef HAVE_OPENGL
  1891. + if(config->use_opengl)
  1892. + {
  1893. + // setup opengl state and transform
  1894. + glDisable(GL_DEPTH_TEST);
  1895. + glDisable(GL_CULL_FACE);
  1896. + glEnable(GL_TEXTURE_2D);
  1897. + glEnable(GL_BLEND);
  1898. + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1899. - glViewport(0, 0, screen->w, screen->h);
  1900. - glMatrixMode(GL_PROJECTION);
  1901. - glLoadIdentity();
  1902. - // logical resolution here not real monitor resolution
  1903. - glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  1904. - glMatrixMode(GL_MODELVIEW);
  1905. - glLoadIdentity();
  1906. - glTranslatef(0, 0, 0);
  1907. + glViewport(0, 0, screen->w, screen->h);
  1908. + glMatrixMode(GL_PROJECTION);
  1909. + glLoadIdentity();
  1910. + // logical resolution here not real monitor resolution
  1911. + glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
  1912. + glMatrixMode(GL_MODELVIEW);
  1913. + glLoadIdentity();
  1914. + glTranslatef(0, 0, 0);
  1915. - check_gl_error("Setting up view matrices");
  1916. + check_gl_error("Setting up view matrices");
  1917. + }
  1918. +#endif
  1919. if(texture_manager != NULL)
  1920. texture_manager->reload_textures();
  1921. Index: configure.ac
  1922. ===================================================================
  1923. --- configure.ac (revision 5071)
  1924. +++ configure.ac (working copy)
  1925. @@ -154,9 +154,15 @@
  1926. [AC_MSG_ERROR([Please install OpenAL])],
  1927. [], [])
  1928. -AX_CHECK_GL
  1929. -if test "$no_gl" = "yes"; then
  1930. - AC_MSG_ERROR([Please install opengl libraries and headers])
  1931. +AC_ARG_ENABLE(opengl,
  1932. + AC_HELP_STRING([--enable-opengl], [enable opengl support]),
  1933. + [enable_opengl=$enableval], [enable_opengl=yes])
  1934. +
  1935. +if test "$enable_opengl" = "yes"; then
  1936. + AX_CHECK_GL
  1937. + if test "$no_gl" != "yes"; then
  1938. + AC_DEFINE_UNQUOTED(HAVE_OPENGL, 1, Define if OpenGL is present on the system)
  1939. + fi
  1940. fi
  1941. dnl Checks for library functions.