Atrinik Client  4.0
fps.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
32 #include <global.h>
33 
37 typedef struct widget_fps_struct {
41  uint32_t lasttime;
42 
46  uint32_t current;
47 
51  uint32_t current_real;
52 
56  uint32_t frames;
57 
61  uint32_t frames_real;
63 
65 static void widget_draw(widgetdata *widget)
66 {
67  widget_fps_struct *tmp;
68 
69  if (!widget->redraw) {
70  return;
71  }
72 
73  tmp = widget->subwidget;
74 
75  text_show_format(widget->surface, FONT_ARIAL11, 4, 4, COLOR_WHITE, 0, NULL,
76  "%d (%d)", tmp->current, tmp->current_real);
77 }
78 
80 static void widget_background(widgetdata *widget, int draw)
81 {
82  widget_fps_struct *tmp;
83  uint32_t ticks;
84 
85  tmp = widget->subwidget;
86  tmp->frames++;
87  tmp->frames_real += draw;
88  ticks = SDL_GetTicks();
89 
90  if (tmp->lasttime < ticks - 1000) {
91  if (tmp->current != tmp->frames ||
92  tmp->current_real != tmp->frames_real) {
93  widget->redraw = 1;
94  }
95 
96  tmp->lasttime = ticks;
97  tmp->current = tmp->frames;
98  tmp->current_real = tmp->frames_real;
99  tmp->frames = 0;
100  tmp->frames_real = 0;
101  }
102 }
103 
108 {
109  widget_fps_struct *tmp;
110 
111  widget->draw_func = widget_draw;
112  widget->background_func = widget_background;
113 
114  widget->subwidget = tmp = ecalloc(1, sizeof(*tmp));
115  tmp->lasttime = SDL_GetTicks();
116 }
static uint32_t ticks
Definition: range_buttons.c:38
static void widget_background(widgetdata *widget, int draw)
Definition: fps.c:80
void * subwidget
Definition: widget.h:107
SDL_Surface * surface
Definition: widget.h:110
struct widget_fps_struct widget_fps_struct
static void widget_draw(widgetdata *widget)
Definition: fps.c:65
uint8_t redraw
Definition: widget.h:72
uint32_t frames
Definition: fps.c:56
uint32_t current_real
Definition: fps.c:51
uint32_t current
Definition: fps.c:46
void widget_fps_init(widgetdata *widget)
Definition: fps.c:107
uint32_t frames_real
Definition: fps.c:61
void text_show_format(SDL_Surface *surface, font_struct *font, int x, int y, const char *color_notation, uint64_t flags, SDL_Rect *box, const char *format,...)
Definition: text.c:2289
uint32_t lasttime
Definition: fps.c:41
#define COLOR_WHITE
Definition: text.h:289