-
Notifications
You must be signed in to change notification settings - Fork 4
/
ballmanager.cpp
122 lines (104 loc) · 3.68 KB
/
ballmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* ballmanager.cpp
* kattMickisShooter
*
* Created by Erik Andersson on 2009-12-19.
* Copyright (c) 2010 Erik Andersson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "SDL_image.h"
#include "ball.h"
#include "mysdl.h"
#include "ballmanager.h"
SDL_Surface *BallManager::_image = NULL;
SDL_Surface *BallManager::_balls[BALLMANAGER_COLORS] = {NULL, NULL, NULL, NULL, NULL, NULL};
std::vector<Ball::Colors> BallManager::_available = std::vector<Ball::Colors>();
SDL_Surface *BallManager::load(Ball::Colors color)
{
// Load first image
if ( _image == NULL )
{
SDL_Surface * image = IMG_Load("ball.png");
if ( ! image )
{
std::cerr << "Could not load image: " << IMG_GetError() << std::endl;
return NULL;
}
SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 0xFF, 0xFF, 0xFF));
_image = image;
}
// Create single image (if needed)
int index = color - 1;
if ( ! _balls[index] )
{
// Make copy of the ball that we can enlarge
SDL_PixelFormat *format = _image->format;
SDL_Surface *ball = SDL_CreateRGBSurface(_image->flags,
BALL_WIDTH, BALL_HEIGHT, format->BitsPerPixel,
format->Rmask, format->Bmask, format->Gmask, format->Amask);
// 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
//SDL_FillRect(ball, NULL, SDL_MapRGBA(format, 0x00, 0x00, 0x00, 0xFF));
SDL_Rect rect;
rect.y = 0;
switch ( color )
{
case Ball::Red: rect.x = 0; break;
case Ball::Green: rect.x = 23; break;
case Ball::Yellow: rect.x = 46; break;
case Ball::Blue: rect.x = 69; break;
case Ball::Teal: rect.x = 92; break;
case Ball::Purple: rect.x = 115; break;
default: std::cout << "Sanity check failed" << std::endl;
}
draw_transparent_surface_onto_empty_surface(rect, ball, _image);
_balls[index] = ball;
}
return _balls[index];
}
Ball::Colors BallManager::randomColor()
{
Ball::Colors color;
int random = rand() % 6 + 1;
color = (Ball::Colors)random;
return color;
}
void BallManager::prepRemList(Grid *grid)
{
// Clear old availables list
_available.erase(_available.begin(), _available.end());
ball_list balls = grid->balls();
for ( ball_list::iterator iter = balls.begin(); iter != balls.end(); iter++ )
if ( count(_available.begin(), _available.end(), (*iter)->color()) == 0 )
_available.push_back((*iter)->color());
}
Ball::Colors BallManager::randomRemainingColor(Grid *grid)
{
prepRemList(grid);
std::random_shuffle(_available.begin(), _available.end());
return _available.front();
}
bool BallManager::colorExists(Ball::Colors check)
{
return count(_available.begin(), _available.end(), check);
}