source: abuse/branches/lol/src/profile.cpp

Last change on this file was 732, checked in by Sam Hocevar, 8 years ago

build: SDL2 compilation fixes.

File size: 2.9 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
5 *
6 *  This software was released into the Public Domain. As with most public
7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "imlib/window.h"
18
19#include "game.h"
20#include "profile.h"
21#include "property.h"
22#include "objects.h"
23
24
25AWindow *prof_win=NULL;
26int prof_height=10;
27
28struct prof_info
29{
30  uint16_t otype;
31  float total_time;
32};
33
34
35prof_info *prof_list=NULL;
36
37
38int profiling() { return prof_list!=NULL; }
39
40void profile_toggle()
41{
42  if (prof_win) { profile_uninit(); }
43  else profile_init();
44}
45
46int profile_handle_event(Event &ev)
47{
48  if (ev.type==EV_CLOSE_WINDOW && ev.window==prof_win)
49  {
50    profile_toggle();
51    return 1;
52  } else return 0;
53}
54
55void profile_init()
56{
57  if (prof_list) { profile_uninit(); }
58  prof_list=(prof_info *)malloc(sizeof(prof_info)*total_objects);
59  profile_reset();
60
61  prof_win = wm->CreateWindow(ivec2(g_prop->getd("profile x", -1),
62                                    g_prop->getd("profile y", -1)),
63                              ivec2(20, prof_height + 1) * console_font->Size(),
64                              "PROFILE");
65}
66
67
68void profile_reset()
69{
70  int i;
71  for (i=0; i<total_objects; i++)
72  {
73    prof_list[i].otype=i;
74    prof_list[i].total_time=0;
75  }
76
77}
78
79
80void profile_uninit()
81{
82  if (prof_list) free(prof_list);
83  prof_list=NULL;
84  if (prof_win) { wm->close_window(prof_win); prof_win=NULL; }
85}
86
87
88void profile_add_time(int type, float amount)
89{
90  if (prof_list)
91  { prof_list[type].total_time+=amount; }
92}
93
94static int p_sorter(const void *a, const void *b)
95{
96  if (((prof_info const *)a)->total_time<((prof_info const *)b)->total_time)
97    return 1;
98  else if (((prof_info const *)a)->total_time>((prof_info const *)b)->total_time)
99    return -1;
100  else return 0;
101}
102
103static void profile_sort()
104{
105  qsort(prof_list,total_objects,sizeof(prof_info),p_sorter);
106}
107
108
109void profile_update()
110{
111  profile_sort();
112  if (prof_list[0].total_time<=0.0) return ;     // nothing took any time!
113
114  int i=0;
115  int spliter=(prof_win->x2()+prof_win->x1())/2;
116  int max_bar_length=spliter-prof_win->x1();
117
118
119  float time_scaler=(float)max_bar_length/prof_list[0].total_time;
120
121  prof_win->m_surf->Bar(ivec2(0, prof_win->y1()),
122                        ivec2(prof_win->m_surf->Size().x - 1,
123                              prof_win->m_surf->Size().y), 0);
124  int dy = 0;
125  for (; i<prof_height; i++)
126  {
127    console_font->PutString(prof_win->m_surf, ivec2(spliter + 1, dy), object_names[prof_list[i].otype]);
128    prof_win->m_surf->Bar(ivec2(spliter - 1 - (int)(prof_list[i].total_time * time_scaler), dy + 1),
129                          ivec2(spliter - 1, dy + console_font->Size().y - 1),
130                          wm->bright_color());
131    dy+=console_font->Size().y+1;
132  }
133}
134
Note: See TracBrowser for help on using the repository browser.