ObjectivelyMVC 0.1.0
Object oriented MVC framework for OpenGL, SDL2 and GNU C
Macros | Functions
Renderer.c File Reference
#include <assert.h>
#include "Log.h"
#include "Renderer.h"
#include "View.h"
#include "Window.h"

Go to the source code of this file.

Macros

#define _Class   _Renderer
 

Functions

Class * _Renderer (void)
 
static void beginFrame (Renderer *self)
 
static GLuint createTexture (const Renderer *self, const SDL_Surface *surface)
 
static void drawLine (const Renderer *self, const SDL_Point *points)
 
static void drawLines (const Renderer *self, const SDL_Point *points, size_t count)
 
static void drawRect (const Renderer *self, const SDL_Rect *rect)
 
static void drawRectFilled (const Renderer *self, const SDL_Rect *rect)
 
static void drawTexture (const Renderer *self, GLuint texture, const SDL_Rect *rect)
 
static void drawView (Renderer *self, View *view)
 
static void endFrame (Renderer *self)
 
static Rendererinit (Renderer *self)
 
static void initialize (Class *clazz)
 
static void renderDeviceDidReset (Renderer *self)
 
static void renderDeviceWillReset (Renderer *self)
 
static void setClippingFrame (Renderer *self, const SDL_Rect *clippingFrame)
 
static void setDrawColor (Renderer *self, const SDL_Color *color)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Renderer

Definition at line 31 of file Renderer.c.

Function Documentation

◆ _Renderer()

Class * _Renderer ( void  )

Definition at line 312 of file Renderer.c.

312 {
313 static Class *clazz;
314 static Once once;
315
316 do_once(&once, {
317 clazz = _initialize(&(const ClassDef) {
318 .name = "Renderer",
319 .superclass = _Object(),
320 .instanceSize = sizeof(Renderer),
321 .interfaceOffset = offsetof(Renderer, interface),
322 .interfaceSize = sizeof(RendererInterface),
324 });
325 });
326
327 return clazz;
328}
static void initialize(Class *clazz)
Definition: Renderer.c:290
The Renderer is responsible for rasterizing the View hierarchy of a WindowController.
Definition: Renderer.h:50

◆ beginFrame()

static void beginFrame ( Renderer self)
static

Definition at line 39 of file Renderer.c.

39 {
40
41 glEnable(GL_BLEND);
42 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
43
44 glEnable(GL_SCISSOR_TEST);
45
46 glEnableClientState(GL_VERTEX_ARRAY);
47 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
48
49 $(self, setDrawColor, &Colors.White);
50}
W3C Color constants.
Definition: Colors.h:37
SDL_Color White
Definition: Colors.h:185
void setDrawColor(Renderer *self, const SDL_Color *color)
Sets the primary color for drawing operations.
Definition: Renderer.c:281

◆ createTexture()

static GLuint createTexture ( const Renderer self,
const SDL_Surface *  surface 
)
static

Definition at line 56 of file Renderer.c.

56 {
57
58 assert(surface);
59
60 GLenum format;
61 switch (surface->format->BytesPerPixel) {
62 case 1:
63 format = GL_LUMINANCE;
64 break;
65 case 3:
66 format = GL_RGB;
67 break;
68 case 4:
69 format = GL_RGBA;
70 break;
71 default:
72 MVC_LogError("Invalid surface format: %s\n", SDL_GetPixelFormatName(surface->format->format));
73 return 0;
74 }
75
76 GLuint texture;
77 glGenTextures(1, &texture);
78
79 glBindTexture(GL_TEXTURE_2D, texture);
80
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
83 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
84
85 glTexImage2D(GL_TEXTURE_2D, 0, format, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
86
87 return texture;
88}
#define MVC_LogError(fmt,...)
Definition: Log.h:55

◆ drawLine()

static void drawLine ( const Renderer self,
const SDL_Point *  points 
)
static

Definition at line 94 of file Renderer.c.

94 {
95
96 assert(points);
97
98 $(self, drawLines, points, 2);
99}
void drawLines(const Renderer *self, const SDL_Point *points, size_t count)
Draws line segments between adjacent points using GL_LINE_STRIP.
Definition: Renderer.c:105

◆ drawLines()

static void drawLines ( const Renderer self,
const SDL_Point *  points,
size_t  count 
)
static

Definition at line 105 of file Renderer.c.

105 {
106
107 assert(points);
108
109 glVertexPointer(2, GL_INT, 0, points);
110
111 glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) count);
112}

◆ drawRect()

static void drawRect ( const Renderer self,
const SDL_Rect *  rect 
)
static

Definition at line 118 of file Renderer.c.

118 {
119
120 assert(rect);
121
122 GLint verts[8];
123
124 verts[0] = rect->x;
125 verts[1] = rect->y;
126
127 verts[2] = rect->x + rect->w;
128 verts[3] = rect->y;
129
130 verts[4] = rect->x + rect->w;
131 verts[5] = rect->y + rect->h;
132
133 verts[6] = rect->x;
134 verts[7] = rect->y + rect->h;
135
136 glVertexPointer(2, GL_INT, 0, verts);
137 glDrawArrays(GL_LINE_LOOP, 0, 4);
138}

◆ drawRectFilled()

static void drawRectFilled ( const Renderer self,
const SDL_Rect *  rect 
)
static

Definition at line 144 of file Renderer.c.

144 {
145
146 assert(rect);
147
148 glRecti(rect->x - 1, rect->y - 1, rect->x + rect->w + 1, rect->y + rect->h + 1);
149}

◆ drawTexture()

static void drawTexture ( const Renderer self,
GLuint  texture,
const SDL_Rect *  rect 
)
static

Definition at line 155 of file Renderer.c.

155 {
156
157 assert(rect);
158
159 const GLfloat texcoords[] = {
160 0.0, 0.0,
161 1.0, 0.0,
162 1.0, 1.0,
163 0.0, 1.0
164 };
165
166 GLint verts[8];
167
168 verts[0] = rect->x;
169 verts[1] = rect->y;
170
171 verts[2] = rect->x + rect->w;
172 verts[3] = rect->y;
173
174 verts[4] = rect->x + rect->w;
175 verts[5] = rect->y + rect->h;
176
177 verts[6] = rect->x;
178 verts[7] = rect->y + rect->h;
179
180 glEnable(GL_TEXTURE_2D);
181 glBindTexture(GL_TEXTURE_2D, texture);
182
183 glVertexPointer(2, GL_INT, 0, verts);
184 glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
185
186 glDrawArrays(GL_QUADS, 0, 4);
187
188 glDisable(GL_TEXTURE_2D);
189}

◆ drawView()

static void drawView ( Renderer self,
View view 
)
static

Definition at line 195 of file Renderer.c.

195 {
196
197 assert(view);
198
199 const SDL_Rect clippingFrame = $(view, clippingFrame);
200 if (clippingFrame.w && clippingFrame.h) {
201
203
204 $(view, render, self);
205 }
206}
void setClippingFrame(Renderer *self, const SDL_Rect *clippingFrame)
Sets the clipping frame for draw operations.
Definition: Renderer.c:260
SDL_Rect clippingFrame(const View *self)
Definition: View.c:429
void render(View *self, Renderer *renderer)
Renders this View using the given renderer.
Definition: Control.c:131

◆ endFrame()

static void endFrame ( Renderer self)
static

Definition at line 212 of file Renderer.c.

212 {
213
214 $(self, setDrawColor, &Colors.White);
215
216 glDisableClientState(GL_VERTEX_ARRAY);
217 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
218
219 $(self, setClippingFrame, NULL);
220
221 glDisable(GL_SCISSOR_TEST);
222
223 glBlendFunc(GL_ONE, GL_ZERO);
224 glDisable(GL_BLEND);
225
226 const GLenum err = glGetError();
227 if (err) {
228 MVC_LogError("GL error: %d\n", err);
229 }
230}

◆ init()

static Renderer * init ( Renderer self)
static

Definition at line 236 of file Renderer.c.

236 {
237 return (Renderer *) super(Object, self, init);
238}
CollectionView * init(CollectionView *self, const SDL_Rect *frame)
Initializes this CollectionView with the specified frame and style.

◆ initialize()

static void initialize ( Class *  clazz)
static
See also
Class::initialize(Class *)

Definition at line 290 of file Renderer.c.

290 {
291
292 ((RendererInterface *) clazz->interface)->beginFrame = beginFrame;
293 ((RendererInterface *) clazz->interface)->createTexture = createTexture;
294 ((RendererInterface *) clazz->interface)->drawLine = drawLine;
295 ((RendererInterface *) clazz->interface)->drawLines = drawLines;
296 ((RendererInterface *) clazz->interface)->drawRect = drawRect;
297 ((RendererInterface *) clazz->interface)->drawRectFilled = drawRectFilled;
298 ((RendererInterface *) clazz->interface)->drawTexture = drawTexture;
299 ((RendererInterface *) clazz->interface)->drawView = drawView;
300 ((RendererInterface *) clazz->interface)->endFrame = endFrame;
301 ((RendererInterface *) clazz->interface)->init = init;
302 ((RendererInterface *) clazz->interface)->renderDeviceDidReset = renderDeviceDidReset;
303 ((RendererInterface *) clazz->interface)->renderDeviceWillReset = renderDeviceWillReset;
304 ((RendererInterface *) clazz->interface)->setClippingFrame = setClippingFrame;
305 ((RendererInterface *) clazz->interface)->setDrawColor = setDrawColor;
306}
void renderDeviceDidReset(Font *self)
This method should be invoked when the render context is invalidated.
Definition: Font.c:247
GLuint createTexture(const Renderer *self, const SDL_Surface *surface)
Generates and binds to an OpenGL texture object, uploading the given surface.
Definition: Renderer.c:56
void endFrame(Renderer *self)
Definition: Renderer.c:212
void drawRectFilled(const Renderer *self, const SDL_Rect *rect)
Fills a rectangle using glRecti.
Definition: Renderer.c:144
void drawRect(const Renderer *self, const SDL_Rect *rect)
Draws a rectangle using GL_LINE_LOOP.
Definition: Renderer.c:118
void renderDeviceWillReset(Renderer *self)
This method is invoked when the render device will become reset.
Definition: Renderer.c:252
void drawLine(const Renderer *self, const SDL_Point *points)
Draws a line segment between two points using GL_LINE_STRIP.
Definition: Renderer.c:94
void drawTexture(const Renderer *self, GLuint texture, const SDL_Rect *dest)
Draws a textured GL_QUAD in the given rectangle.
Definition: Renderer.c:155
void drawView(Renderer *self, View *view)
Draws the given View, setting the clipping frame and invoking View::render.
Definition: Renderer.c:195
void beginFrame(Renderer *self)
Sets up OpenGL state.
Definition: Renderer.c:39
void renderDeviceDidReset(View *self)
Informs this View that the render device has reset.
Definition: Text.c:172

◆ renderDeviceDidReset()

static void renderDeviceDidReset ( Renderer self)
static

Definition at line 244 of file Renderer.c.

244 {
245
246}

◆ renderDeviceWillReset()

static void renderDeviceWillReset ( Renderer self)
static

Definition at line 252 of file Renderer.c.

252 {
253
254}

◆ setClippingFrame()

static void setClippingFrame ( Renderer self,
const SDL_Rect *  clippingFrame 
)
static

Definition at line 260 of file Renderer.c.

260 {
261
262 SDL_Window *window = SDL_GL_GetCurrentWindow();
263
264 SDL_Rect rect;
265 if (clippingFrame) {
266 rect = *clippingFrame;
267 } else {
268 rect = MakeRect(0, 0, 0, 0);
269 SDL_GL_GetDrawableSize(window, &rect.w, &rect.h);
270 }
271
272 const SDL_Rect scissor = MVC_TransformToWindow(window, &rect);
273
274 glScissor(scissor.x - 1, scissor.y - 1, scissor.w + 2, scissor.h + 2);
275}
#define MakeRect(x, y, w, h)
Creates an SDL_Rect with the given origin and size.
Definition: Types.h:74
SDL_Rect MVC_TransformToWindow(SDL_Window *window, const SDL_Rect *rect)
Transforms the specified rectangle to normalized device coordinates in window.
Definition: Window.c:28

◆ setDrawColor()

static void setDrawColor ( Renderer self,
const SDL_Color *  color 
)
static

Definition at line 281 of file Renderer.c.

281 {
282 glColor4ubv((const GLubyte *) color);
283}