source: abuse/trunk/src/profile.cpp @ 494

Last change on this file since 494 was 494, checked in by Sam Hocevar, 11 years ago

style: remove trailing spaces, fix copyright statements.

File size: 2.7 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *  Copyright (c) 2005-2011 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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include "game.h"
14
15
16#include "profile.h"
17#include "jwindow.h"
18#include "property.h"
19#include "objects.h"
20
21
22Jwindow *prof_win=NULL;
23int prof_height=10;
24
25struct prof_info
26{
27  uint16_t otype;
28  float total_time;
29};
30
31
32prof_info *prof_list=NULL;
33
34
35int profiling() { return prof_list!=NULL; }
36
37void profile_toggle()
38{
39  if (prof_win) { profile_uninit(); }
40  else profile_init();
41}
42
43int profile_handle_event(event &ev)
44{
45  if (ev.type==EV_CLOSE_WINDOW && ev.window==prof_win)
46  {
47    profile_toggle();
48    return 1;
49  } else return 0;
50}
51
52void profile_init()
53{
54  if (prof_list) { profile_uninit(); }
55  prof_list=(prof_info *)malloc(sizeof(prof_info)*total_objects);
56  profile_reset();
57
58
59  prof_win=wm->new_window(prop->getd("profile x",-1),
60              prop->getd("profile y",-1),
61              20*console_font->width(),
62              (prof_height+1)*console_font->height(),
63              NULL,
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 *)a)->total_time<((prof_info *)b)->total_time)
97    return 1;
98  else if (((prof_info *)a)->total_time>((prof_info *)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->screen->bar(0,prof_win->y1(),prof_win->screen->width()-1,prof_win->screen->height(),0);
122  int dy = 0;
123  for (; i<prof_height; i++)
124  {
125    console_font->put_string(prof_win->screen,spliter+1,dy,object_names[prof_list[i].otype]);
126    prof_win->screen->bar(spliter-1-(int)(prof_list[i].total_time*time_scaler),dy+1,
127              spliter-1,
128              dy+console_font->height()-1,wm->bright_color());
129    dy+=console_font->height()+1;
130  }
131
132}
133
134
135
136
137
138
Note: See TracBrowser for help on using the repository browser.