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

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

core: rename vec2i to ivec2 and update matrix.h from Lol Engine.

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-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, by
8 *  Jonathan Clark, or by Sam Hocevar.
9 */
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "common.h"
16
17#include "game.h"
18
19#include "profile.h"
20#include "jwindow.h"
21#include "property.h"
22#include "objects.h"
23
24
25Jwindow *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
62  prof_win=wm->CreateWindow(ivec2(prop->getd("profile x", -1),
63                                  prop->getd("profile y", -1)),
64                            ivec2(20, prof_height + 1) * console_font->Size(),
65                            NULL, "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->m_surf->Bar(ivec2(0, prof_win->y1()),
123                        ivec2(prof_win->m_surf->Size().x - 1,
124                              prof_win->m_surf->Size().y), 0);
125  int dy = 0;
126  for (; i<prof_height; i++)
127  {
128    console_font->PutString(prof_win->m_surf, ivec2(spliter + 1, dy), object_names[prof_list[i].otype]);
129    prof_win->m_surf->Bar(ivec2(spliter - 1 - (int)(prof_list[i].total_time * time_scaler), dy + 1),
130                          ivec2(spliter - 1, dy + console_font->Size().y - 1),
131                          wm->bright_color());
132    dy+=console_font->Size().y+1;
133  }
134}
135
Note: See TracBrowser for help on using the repository browser.