DEADSOFTWARE

3535a2d2969b02b4559b2fdef0bf5e870aa4259c
[d2df-sdl.git] / src / game / opengl / r_render.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE ../../shared/a_modes.inc}
16 unit r_render;
18 interface
20 procedure r_Render_Initialize;
21 procedure r_Render_Finalize;
23 procedure r_Render_Load;
24 procedure r_Render_Free;
26 procedure r_Render_LoadTextures;
27 procedure r_Render_FreeTextures;
29 procedure r_Render_Update;
31 procedure r_Render_Resize (w, h: Integer);
32 procedure r_Render_Apply;
34 function r_Render_WriteScreenShot (filename: String): Boolean;
36 implementation
38 uses
39 {$INCLUDE ../../nogl/noGLuses.inc}
40 SysUtils, Classes, Math,
41 e_log, g_system, utils,
42 g_game, g_options, g_console,
43 r_window, r_graphics, r_console, r_playermodel, r_textures, r_animations,
44 r_weapons, r_items, r_gfx, r_monsters, r_map, r_player, r_game
45 ;
47 var
48 LoadedGL: Boolean = false;
50 procedure LoadGL;
51 begin
52 if LoadedGL = false then
53 begin
54 {$IFDEF NOGL_INIT}
55 nogl_Init;
56 if glRenderToFBO and (not nogl_ExtensionSupported('GL_OES_framebuffer_object')) then
57 begin
58 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
59 glRenderToFBO := false
60 end;
61 {$ELSE}
62 if glRenderToFBO and (not Load_GL_ARB_framebuffer_object) then
63 begin
64 e_LogWriteln('GL: framebuffer objects not supported; disabling FBO rendering');
65 glRenderToFBO := false
66 end;
67 {$ENDIF}
68 LoadedGL := true
69 end
70 end;
72 procedure FreeGL;
73 begin
74 if LoadedGL = true then
75 begin
76 {$IFDEF NOGL_INIT}
77 nogl_Quit;
78 {$ENDIF}
79 LoadedGL := false
80 end
81 end;
83 procedure r_Render_LoadTextures;
84 begin
85 r_Game_LoadTextures;
86 r_Map_LoadTextures;
87 end;
89 procedure r_Render_FreeTextures;
90 begin
91 r_Map_FreeTextures;
92 r_Game_FreeTextures;
93 end;
95 procedure r_Render_Load;
96 begin
97 r_Game_Load; // load first!
98 r_Player_Load;
99 r_Map_Load;
100 r_PlayerModel_Load;
101 r_Monsters_Load;
102 r_Weapon_Load;
103 r_Items_Load;
104 r_GFX_Load;
105 end;
107 procedure r_Render_Free;
108 begin
109 r_GFX_Free;
110 r_Items_Free;
111 r_Weapon_Free;
112 r_Monsters_Free;
113 r_PlayerModel_Free;
114 r_Map_Free;
115 r_Player_Free;
116 r_Game_Free;
117 g_Texture_DeleteAll;
118 g_Frames_DeleteAll;
119 end;
121 procedure r_Render_Initialize;
122 begin
123 if sys_SetDisplayMode(gRC_Width, gRC_Height, gBPP, gRC_FullScreen, gRC_Maximized) = False then
124 raise Exception.Create('Failed to set videomode on startup.');
125 LoadGL;
126 r_Window_Initialize;
127 r_Console_Init;
128 r_PlayerModel_Initialize;
129 r_Map_Initialize;
130 end;
132 procedure r_Render_Finalize;
133 begin
134 r_Map_Finalize;
135 r_PlayerModel_Finalize;
136 FreeGL;
137 e_ReleaseEngine
138 end;
140 procedure r_Render_Update;
141 begin
142 r_Map_Update;
143 r_PlayerModel_Update;
144 end;
146 procedure r_Render_Resize (w, h: Integer);
147 begin
148 LoadGL;
149 gWinSizeX := w;
150 gWinSizeY := h;
151 gRC_Width := w;
152 gRC_Height := h;
153 if glRenderToFBO then
154 begin
155 // store real window size in gWinSize, downscale resolution now
156 w := round(w / r_pixel_scale);
157 h := round(h / r_pixel_scale);
158 if not e_ResizeFramebuffer(w, h) then
159 begin
160 e_LogWriteln('GL: could not create framebuffer, falling back to --no-fbo');
161 glRenderToFBO := False;
162 w := gWinSizeX;
163 h := gWinSizeY;
164 end;
165 end;
166 gScreenWidth := w;
167 gScreenHeight := h;
168 e_ResizeWindow(w, h);
169 e_InitGL;
170 r_Game_SetupScreenSize;
171 end;
173 procedure r_Render_Apply;
174 begin
175 if sys_SetDisplayMode(Max(1, gRC_Width), Max(1, gRC_Height), Max(1, gBPP), gRC_FullScreen, gRC_Maximized) then
176 e_LogWriteln('resolution changed')
177 else
178 e_LogWriteln('resolution not changed');
179 sys_EnableVSync(gVSync)
180 end;
182 function r_Render_WriteScreenShot (filename: String): Boolean;
183 var s: TStream;
184 begin
185 Result := False;
186 try
187 s := CreateDiskFile(filename);
188 try
189 e_MakeScreenshot(s, gWinSizeX, gWinSizeX);
190 Result := True;
191 except
192 DeleteFile(filename)
193 end;
194 s.Free;
195 finally
196 end
197 end;
199 end.