Debian Codegraph @

src/level/ShapeBuilder.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "ShapeBuilder.h"
10
11 #include "Shape.h"
12 #include "View.h"
13 #include "SurfaceTool.h"
14
15 //-----------------------------------------------------------------
16 /**
17 * Prepare color values
18 * based on the given shape and weight.
19 */
20 void
21 ShapeBuilder::prepareColor(SDL_Color *color, const Shape *shape,
22 Cube::eWeight weight)
23 {
24 if (NULL == color) {
25 return;
26 }
27
28 color->r = 0;
29 color->g = 0;
30 color->b = 0;
31 color->unused = 255;
32
33 switch (weight) {
34 case Cube::LIGHT:
35 color->g = ShapeBuilder::calcShapeHash(shape) % 255;
36 color->r = 255 - color->g;
37 break;
38 case Cube::HEAVY:
39 color->b = 50 + ShapeBuilder::calcShapeHash(shape) % (255 - 50);
40 break;
41 default:
42 color->r = 128;
43 color->g = 128;
44 color->b = 128;
45 break;
46 }
47 }
48 //-----------------------------------------------------------------
49 /**
50 * Calc an almost unique hash of the shape.
51 */
52 Uint32
53 ShapeBuilder::calcShapeHash(const Shape *shape)
54 {
55 Uint32 hash = 0;
56 Shape::const_iterator end = shape->marksEnd();
57 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
58 hash = 31 * hash + i->getX();
59 hash = 31 * hash + i->getY();
60 }
61 return hash;
62 }
63 //-----------------------------------------------------------------
64 /**
65 * Create new image for the given shape.
66 */
67 SDL_Surface *
68 ShapeBuilder::createImage(const Shape *shape, Cube::eWeight weight)
69 {
70 static const SDL_Color TRANSPARENT = {255, 0, 255, 255};
71
72 SDL_Surface *surface = SurfaceTool::createTransparent(
73 shape->getW() * View::SCALE, shape->getH() * View::SCALE,
74 TRANSPARENT);
75
76 SDL_Rect rect;
77 rect.w = View::SCALE;
78 rect.h = View::SCALE;
79
80 SDL_Color color;
81 prepareColor(&color, shape, weight);
82
83 Shape::const_iterator end = shape->marksEnd();
84 for (Shape::const_iterator i = shape->marksBegin(); i != end; ++i) {
85 rect.x = i->getX() * View::SCALE;
86 rect.y = i->getY() * View::SCALE;
87 SurfaceTool::alphaFill(surface, &rect, color);
88 }
89
90 return surface;
91 }
92