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

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

build: SDL2 compilation fixes.

File size: 2.9 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
[715]4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This software was released into the Public Domain. As with most public
[555]7 *  domain software, no warranty is made or implied by Crack dot Com, by
8 *  Jonathan Clark, or by Sam Hocevar.
[56]9 */
10
[732]11#if HAVE_CONFIG_H
[555]12#   include "config.h"
13#endif
[56]14
[512]15#include "common.h"
16
[724]17#include "imlib/window.h"
18
[481]19#include "game.h"
20#include "profile.h"
21#include "property.h"
22#include "objects.h"
[2]23
24
[715]25AWindow *prof_win=NULL;
[2]26int prof_height=10;
27
28struct prof_info
29{
[17]30  uint16_t otype;
[2]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
[643]46int profile_handle_event(Event &ev)
[2]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(); }
[129]58  prof_list=(prof_info *)malloc(sizeof(prof_info)*total_objects);
[2]59  profile_reset();
60
[715]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");
[2]65}
66
67
68void profile_reset()
69{
70  int i;
[494]71  for (i=0; i<total_objects; i++)
[2]72  {
73    prof_list[i].otype=i;
74    prof_list[i].total_time=0;
[124]75  }
76
[2]77}
78
79
80void profile_uninit()
81{
[129]82  if (prof_list) free(prof_list);
[2]83  prof_list=NULL;
[106]84  if (prof_win) { wm->close_window(prof_win); prof_win=NULL; }
[2]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{
[715]96  if (((prof_info const *)a)->total_time<((prof_info const *)b)->total_time)
[2]97    return 1;
[715]98  else if (((prof_info const *)a)->total_time>((prof_info const *)b)->total_time)
[2]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;
[124]120
[682]121  prof_win->m_surf->Bar(ivec2(0, prof_win->y1()),
122                        ivec2(prof_win->m_surf->Size().x - 1,
[655]123                              prof_win->m_surf->Size().y), 0);
[124]124  int dy = 0;
[494]125  for (; i<prof_height; i++)
[2]126  {
[682]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),
[655]130                          wm->bright_color());
[668]131    dy+=console_font->Size().y+1;
[2]132  }
133}
134
Note: See TracBrowser for help on using the repository browser.