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

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