#include <algorithm>
#include <cassert>
#include <iostream>
+#include <SDL_image.h>
+#include <GL/gl.h>
+#include <GL/glu.h>
-#include "drawing_context.h"
-#include "surface.h"
-#include "font.h"
-#include "main.h"
-#include "gameconfig.h"
+#include "drawing_context.hpp"
+#include "surface.hpp"
+#include "font.hpp"
+#include "main.hpp"
+#include "gameconfig.hpp"
+#include "glutil.hpp"
+#include "texture.hpp"
-DrawingContext::DrawingContext(SDL_Surface* targetsurface)
+#define LIGHTMAP_DIV 4
+
+static inline int next_po2(int val)
{
- if(targetsurface) {
- screen = targetsurface;
- } else {
- screen = SDL_GetVideoSurface();
- }
+ int result = 1;
+ while(result < val)
+ result *= 2;
+
+ return result;
+}
+
+DrawingContext::DrawingContext()
+{
+ screen = SDL_GetVideoSurface();
+
+ lightmap_width = screen->w / LIGHTMAP_DIV;
+ lightmap_height = screen->h / LIGHTMAP_DIV;
+ int width = next_po2(lightmap_width);
+ int height = next_po2(lightmap_height);
+
+ lightmap.reset(new Texture(width, height, GL_RGB));
+
+ lightmap_uv_right = static_cast<float>(lightmap_width) / static_cast<float>(width);
+ lightmap_uv_bottom = static_cast<float>(lightmap_height) / static_cast<float>(height);
+
+ requests = &drawing_requests;
}
DrawingContext::~DrawingContext()
request.alpha = transform.alpha;
request.request_data = const_cast<Surface*> (surface);
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
}
request.request_data = surfacepartrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
textrequest->alignment = alignment;
request.request_data = textrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
gradientrequest->bottom = bottom;
request.request_data = gradientrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
* ((float) transform.alpha / 255.0));
request.request_data = fillrectrequest;
- drawingrequests.push_back(request);
+ requests->push_back(request);
}
void
const Color& top = gradientrequest->top;
const Color& bottom = gradientrequest->bottom;
-#ifndef NOOPENGL
- if(config->use_gl)
- {
- glBegin(GL_QUADS);
- glColor3ub(top.red, top.green, top.blue);
- glVertex2f(0, 0);
- glVertex2f(SCREEN_WIDTH, 0);
- glColor3ub(bottom.red, bottom.green, bottom.blue);
- glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
- glVertex2f(0, SCREEN_HEIGHT);
- glEnd();
- }
- else
- {
-#endif
- if(&top == &bottom)
- {
- fillrect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, top.red, top.green, top.blue);
- }
- else
- {
- float redstep = (float(bottom.red)-float(top.red)) / float(SCREEN_HEIGHT);
- float greenstep = (float(bottom.green)-float(top.green)) / float(SCREEN_HEIGHT);
- float bluestep = (float(bottom.blue) - float(top.blue)) / float(SCREEN_HEIGHT);
-
- for(float y = 0; y < SCREEN_HEIGHT; y += 2)
- fillrect(0, (int)y, SCREEN_WIDTH, 2,
- int(float(top.red) + redstep * y),
- int(float(top.green) + greenstep * y),
- int(float(top.blue) + bluestep * y), 255);
- }
-#ifndef NOOPENGL
-
- }
-#endif
+ glBegin(GL_QUADS);
+ glColor3ub(top.red, top.green, top.blue);
+ glVertex2f(0, 0);
+ glVertex2f(SCREEN_WIDTH, 0);
+ glColor3ub(bottom.red, bottom.green, bottom.blue);
+ glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
+ glVertex2f(0, SCREEN_HEIGHT);
+ glEnd();
delete gradientrequest;
}
float w = fillrectrequest->size.x;
float h = fillrectrequest->size.y;
-#ifndef NOOPENGL
- if(config->use_gl)
- {
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue, fillrectrequest->color.alpha);
-
- glBegin(GL_POLYGON);
- glVertex2f(x, y);
- glVertex2f(x+w, y);
- glVertex2f(x+w, y+h);
- glVertex2f(x, y+h);
- glEnd();
- glDisable(GL_BLEND);
- }
- else
- {
-#endif
- SDL_Rect src, rect;
- SDL_Surface *temp = NULL;
-
- rect.x = (int)x;
- rect.y = (int)y;
- rect.w = (int)w;
- rect.h = (int)h;
-
- if(fillrectrequest->color.alpha != 255)
- {
- temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel,
- screen->format->Rmask,
- screen->format->Gmask,
- screen->format->Bmask,
- screen->format->Amask);
-
-
- src.x = 0;
- src.y = 0;
- src.w = rect.w;
- src.h = rect.h;
-
- SDL_FillRect(temp, &src, SDL_MapRGB(screen->format,
- fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue));
-
- SDL_SetAlpha(temp, SDL_SRCALPHA, fillrectrequest->color.alpha);
-
- SDL_BlitSurface(temp,0,screen,&rect);
-
- SDL_FreeSurface(temp);
- }
- else
- SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format,
- fillrectrequest->color.red, fillrectrequest->color.green,
- fillrectrequest->color.blue));
-
-#ifndef NOOPENGL
-
- }
-#endif
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glColor4ub(fillrectrequest->color.red, fillrectrequest->color.green,
+ fillrectrequest->color.blue, fillrectrequest->color.alpha);
+
+ glBegin(GL_POLYGON);
+ glVertex2f(x, y);
+ glVertex2f(x+w, y);
+ glVertex2f(x+w, y+h);
+ glVertex2f(x, y+h);
+ glEnd();
+ glDisable(GL_BLEND);
delete fillrectrequest;
}
{
#ifdef DEBUG
assert(transformstack.empty());
+ assert(target_stack.empty());
#endif
transformstack.clear();
+ target_stack.clear();
+
+ bool use_lightmap = lightmap_requests.size() != 0;
+
+ // PART1: create lightmap
+ if(use_lightmap) {
+ glViewport(0, screen->h - lightmap_height, lightmap_width, lightmap_height);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glClearColor(1, 1, 1, 1);
+ glClear(GL_COLOR_BUFFER_BIT);
+ handle_drawing_requests(lightmap_requests);
+ lightmap_requests.clear();
+
+ glDisable(GL_BLEND);
+ glEnable(GL_TEXTURE_2D);
+ glBindTexture(GL_TEXTURE_2D, lightmap->handle);
+ glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, screen->h - lightmap_height, lightmap_width, lightmap_height);
+
+ glViewport(0, 0, screen->w, screen->h);
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+ }
+
+ //glClear(GL_COLOR_BUFFER_BIT);
+ handle_drawing_requests(drawing_requests);
+ drawing_requests.clear();
+
+ if(use_lightmap) {
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_DST_COLOR, GL_ZERO);
+ //glDisable(GL_BLEND);
+ //glColor4f((float) rand() / (float) RAND_MAX, .22, .88, 1.0f);
+
+ glEnable(GL_TEXTURE_2D);
+ glDisable(GL_DEPTH_TEST);
+ glDisable(GL_CULL_FACE);
+ glDisable(GL_ALPHA_TEST);
+
+ glBindTexture(GL_TEXTURE_2D, lightmap->handle);
+ glBegin(GL_QUADS);
+
+ glTexCoord2f(0, lightmap_uv_bottom);
+ glVertex2f(0, 0);
+
+ glTexCoord2f(lightmap_uv_right, lightmap_uv_bottom);
+ glVertex2f(SCREEN_WIDTH, 0);
+
+ glTexCoord2f(lightmap_uv_right, 0);
+ glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
+
+ glTexCoord2f(0, 0);
+ glVertex2f(0, SCREEN_HEIGHT);
- std::stable_sort(drawingrequests.begin(), drawingrequests.end());
+ glEnd();
+ }
- for(DrawingRequests::iterator i = drawingrequests.begin();
- i != drawingrequests.end(); ++i) {
+ assert_gl("drawing");
+
+ SDL_GL_SwapBuffers();
+}
+
+void
+DrawingContext::handle_drawing_requests(DrawingRequests& requests)
+{
+ std::stable_sort(requests.begin(), requests.end());
+
+ for(DrawingRequests::iterator i = requests.begin();
+ i != requests.end(); ++i) {
switch(i->type) {
case SURFACE:
{
break;
}
}
-
- // update screen
- if(config->use_gl)
- SDL_GL_SwapBuffers();
- else
- SDL_Flip(screen);
-
- drawingrequests.clear();
}
void
{
return transform.alpha;
}
+
+void
+DrawingContext::push_target()
+{
+ target_stack.push_back(target);
+}
+
+void
+DrawingContext::pop_target()
+{
+ set_target(target_stack.back());
+ target_stack.pop_back();
+}
+
+void
+DrawingContext::set_target(Target target)
+{
+ this->target = target;
+ if(target == LIGHTMAP)
+ requests = &lightmap_requests;
+ else
+ requests = &drawing_requests;
+}
+