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 | |
---|
25 | Jwindow *prof_win=NULL; |
---|
26 | int prof_height=10; |
---|
27 | |
---|
28 | struct prof_info |
---|
29 | { |
---|
30 | uint16_t otype; |
---|
31 | float total_time; |
---|
32 | }; |
---|
33 | |
---|
34 | |
---|
35 | prof_info *prof_list=NULL; |
---|
36 | |
---|
37 | |
---|
38 | int profiling() { return prof_list!=NULL; } |
---|
39 | |
---|
40 | void profile_toggle() |
---|
41 | { |
---|
42 | if (prof_win) { profile_uninit(); } |
---|
43 | else profile_init(); |
---|
44 | } |
---|
45 | |
---|
46 | int 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 | |
---|
55 | void 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->new_window(prop->getd("profile x",-1), |
---|
63 | prop->getd("profile y",-1), |
---|
64 | 20*console_font->width(), |
---|
65 | (prof_height+1)*console_font->height(), |
---|
66 | NULL, |
---|
67 | "PROFILE"); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | void profile_reset() |
---|
72 | { |
---|
73 | int i; |
---|
74 | for (i=0; i<total_objects; i++) |
---|
75 | { |
---|
76 | prof_list[i].otype=i; |
---|
77 | prof_list[i].total_time=0; |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | void profile_uninit() |
---|
84 | { |
---|
85 | if (prof_list) free(prof_list); |
---|
86 | prof_list=NULL; |
---|
87 | if (prof_win) { wm->close_window(prof_win); prof_win=NULL; } |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | void profile_add_time(int type, float amount) |
---|
92 | { |
---|
93 | if (prof_list) |
---|
94 | { prof_list[type].total_time+=amount; } |
---|
95 | } |
---|
96 | |
---|
97 | static int p_sorter(const void *a, const void *b) |
---|
98 | { |
---|
99 | if (((prof_info *)a)->total_time<((prof_info *)b)->total_time) |
---|
100 | return 1; |
---|
101 | else if (((prof_info *)a)->total_time>((prof_info *)b)->total_time) |
---|
102 | return -1; |
---|
103 | else return 0; |
---|
104 | } |
---|
105 | |
---|
106 | static void profile_sort() |
---|
107 | { |
---|
108 | qsort(prof_list,total_objects,sizeof(prof_info),p_sorter); |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | void profile_update() |
---|
113 | { |
---|
114 | profile_sort(); |
---|
115 | if (prof_list[0].total_time<=0.0) return ; // nothing took any time! |
---|
116 | |
---|
117 | int i=0; |
---|
118 | int spliter=(prof_win->x2()+prof_win->x1())/2; |
---|
119 | int max_bar_length=spliter-prof_win->x1(); |
---|
120 | |
---|
121 | |
---|
122 | float time_scaler=(float)max_bar_length/prof_list[0].total_time; |
---|
123 | |
---|
124 | prof_win->m_surf->bar(0,prof_win->y1(),prof_win->m_surf->Size().x-1,prof_win->m_surf->Size().y,0); |
---|
125 | int dy = 0; |
---|
126 | for (; i<prof_height; i++) |
---|
127 | { |
---|
128 | console_font->put_string(prof_win->m_surf,spliter+1,dy,object_names[prof_list[i].otype]); |
---|
129 | prof_win->m_surf->bar(spliter-1-(int)(prof_list[i].total_time*time_scaler),dy+1, |
---|
130 | spliter-1, |
---|
131 | dy+console_font->height()-1,wm->bright_color()); |
---|
132 | dy+=console_font->height()+1; |
---|
133 | } |
---|
134 | |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|