File: scoreboard.c

package info (click to toggle)
tuxpuck 0.8.2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 880 kB
  • ctags: 460
  • sloc: ansic: 3,319; makefile: 108
file content (148 lines) | stat: -rw-r--r-- 3,343 bytes parent folder | download | duplicates (8)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* scoreboard.c - Copyright (C) 2001-2002 Jacob Kroon, see COPYING for details */

#include "video.h"
#include "tuxpuck.h"

/* defines */
#define SCOREBOARD_POSITION	((Uint16)460)
#define SCOREBOARD_STATE_IDLE	1
#define SCOREBOARD_STATE_ADD_1	2
#define SCOREBOARD_STATE_ADD_2	3
#define LINE_WIDTH		((Uint32)3)
#define LINE_HEIGHT		((Uint32)22)
#define MOUSEBAR_TIMEOUT	((Uint32)2000)
#define MOUSEBAR_FADOUT_SPEED	((float)0.001)

/* externals */
extern unsigned char scoreboard_png[];
extern unsigned char mousebar_png[];

/* statics */
static SDL_Surface *_sdl_scoreboard, *_sdl_mousebar;
static SDL_Rect _rect, _mousebar_rect;
static Uint32 _counter = 0;
static Uint8 _score[2], _state, _mouse_speed = 0, _p1_scored, _p2_scored;
static float _mousebar_alpha = 0;

/* functions */
static void _blit_point(Uint8 who, Uint8 point)
{
  SDL_Rect rect;

  point -= 1;
  rect.x = SCOREBOARD_POSITION + 18 + point * 8 + (point / 5 - 1) * 4;
  rect.y = 27 + who * 26;
  rect.w = LINE_WIDTH;
  rect.h = LINE_HEIGHT;
  video_fill(video_map_rgb(255, 255, 255), 255, &rect);
}

void scoreboard_init(void)
{
  _score[0] = _score[1] = 0;
  _state = SCOREBOARD_STATE_IDLE;
  _sdl_scoreboard = video_create_png_surface(scoreboard_png, NULL);
  _sdl_mousebar = video_create_png_surface(mousebar_png, NULL);
  _rect.x = SCOREBOARD_POSITION;
  _rect.y = 0;
  _rect.w = _rect.h = 0;
  _p1_scored = _p2_scored = 0;
}

void scoreboard_deinit(void)
{
  SDL_FreeSurface(_sdl_scoreboard);
  SDL_FreeSurface(_sdl_mousebar);
}

void scoreboard_add_point(Uint8 player)
{
  if (player == 1) {
    _state = SCOREBOARD_STATE_ADD_1;
  } else {
    _state = SCOREBOARD_STATE_ADD_2;
  }
}

void scoreboard_blit(void)
{
  video_blit(_sdl_scoreboard, NULL, &_rect);
}

void scoreboard_erase(void)
{
  video_erase(&_rect);
}

void scoreboard_clean_up(void)
{
  if (_mousebar_alpha > 0)
    video_erase(&_mousebar_rect);
}

void scoreboard_set_alpha(Uint8 alpha)
{
  video_set_alpha(_sdl_scoreboard, alpha);
}

void scoreboard_reblit(void)
{
  SDL_Rect rect;

  if (_p1_scored) {
    _blit_point(0, _score[0]);
    _p1_scored = 0;
  } else if (_p2_scored) {
    _blit_point(1, _score[1]);
    _p2_scored = 0;
  }
  if (_mousebar_alpha > 0) {
    rect.x = SCOREBOARD_POSITION + 7;
    rect.y = 96;
    rect.w = (Uint16) (138.0 * _mouse_speed / 10.0);
    rect.h = 13;
    video_fill(video_map_rgb(0, 0, 128), (Uint8) (_mousebar_alpha * 255.0),
	       &rect);
    _mousebar_rect.x = SCOREBOARD_POSITION;
    _mousebar_rect.y = 90;
    video_set_alpha(_sdl_mousebar, (Uint8) (_mousebar_alpha * 255.0));
    video_blit(_sdl_mousebar, NULL, &_mousebar_rect);
  }
}

void scoreboard_update(Uint32 time)
{
  switch (_state) {
  case SCOREBOARD_STATE_IDLE:
    break;
  case SCOREBOARD_STATE_ADD_1:
    _score[0]++;
    _p1_scored = 1;
    _state = SCOREBOARD_STATE_IDLE;
    break;
  case SCOREBOARD_STATE_ADD_2:
    _score[1]++;
    _p2_scored = 1;
    _state = SCOREBOARD_STATE_IDLE;
    break;
  default:
    break;
  }
  if (_mousebar_alpha > 0) {
    _counter += time;
    if (_counter > MOUSEBAR_TIMEOUT) {
      _mousebar_alpha -= time * MOUSEBAR_FADOUT_SPEED;
      if (_mousebar_alpha < 0) {
	_mousebar_alpha = 0;
	video_erase(&_mousebar_rect);
      }
    }
  }
}

void scoreboard_set_mousebar(Uint8 speed)
{
  _mouse_speed = speed;
  _mousebar_alpha = 1.0;
  _counter = 0;
}