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

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

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

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 "common.h"
14
15#include "game.h"
16
17#include "profile.h"
18#include "jwindow.h"
19#include "property.h"
20#include "objects.h"
21
22
23Jwindow *prof_win=NULL;
24int prof_height=10;
25
26struct prof_info
27{
28  uint16_t otype;
29  float total_time;
30};
31
32
33prof_info *prof_list=NULL;
34
35
36int profiling() { return prof_list!=NULL; }
37
38void profile_toggle()
39{
40  if (prof_win) { profile_uninit(); }
41  else profile_init();
42}
43
44int profile_handle_event(event &ev)
45{
46  if (ev.type==EV_CLOSE_WINDOW && ev.window==prof_win)
47  {
48    profile_toggle();
49    return 1;
50  } else return 0;
51}
52
53void profile_init()
54{
55  if (prof_list) { profile_uninit(); }
56  prof_list=(prof_info *)malloc(sizeof(prof_info)*total_objects);
57  profile_reset();
58
59
60  prof_win=wm->new_window(prop->getd("profile x",-1),
61              prop->getd("profile y",-1),
62              20*console_font->width(),
63              (prof_height+1)*console_font->height(),
64              NULL,
65              "PROFILE");
66}
67
68
69void profile_reset()
70{
71  int i;
72  for (i=0; i<total_objects; i++)
73  {
74    prof_list[i].otype=i;
75    prof_list[i].total_time=0;
76  }
77
78}
79
80
81void profile_uninit()
82{
83  if (prof_list) free(prof_list);
84  prof_list=NULL;
85  if (prof_win) { wm->close_window(prof_win); prof_win=NULL; }
86}
87
88
89void profile_add_time(int type, float amount)
90{
91  if (prof_list)
92  { prof_list[type].total_time+=amount; }
93}
94
95static int p_sorter(const void *a, const void *b)
96{
97  if (((prof_info *)a)->total_time<((prof_info *)b)->total_time)
98    return 1;
99  else if (((prof_info *)a)->total_time>((prof_info *)b)->total_time)
100    return -1;
101  else return 0;
102}
103
104static void profile_sort()
105{
106  qsort(prof_list,total_objects,sizeof(prof_info),p_sorter);
107}
108
109
110void profile_update()
111{
112  profile_sort();
113  if (prof_list[0].total_time<=0.0) return ;     // nothing took any time!
114
115  int i=0;
116  int spliter=(prof_win->x2()+prof_win->x1())/2;
117  int max_bar_length=spliter-prof_win->x1();
118
119
120  float time_scaler=(float)max_bar_length/prof_list[0].total_time;
121
122  prof_win->screen->bar(0,prof_win->y1(),prof_win->screen->Size().x-1,prof_win->screen->Size().y,0);
123  int dy = 0;
124  for (; i<prof_height; i++)
125  {
126    console_font->put_string(prof_win->screen,spliter+1,dy,object_names[prof_list[i].otype]);
127    prof_win->screen->bar(spliter-1-(int)(prof_list[i].total_time*time_scaler),dy+1,
128              spliter-1,
129              dy+console_font->height()-1,wm->bright_color());
130    dy+=console_font->height()+1;
131  }
132
133}
134
135
136
137
138
139
Note: See TracBrowser for help on using the repository browser.