Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Can anyone see if im actually coding right.

This thread is locked; no one can reply to it. rss feed Print
Can anyone see if im actually coding right.
dothedru22
Member #7,325
June 2006

So awhile back I was looking through the half-life source code and I just sortof figured this is how they do large scale games. So i decided to start programming sortof like that using a lot of pointers and stuff across a lot of files. it just seems like this is the slow way of doig things. like that im not gettting the most fps out of it. I also changed the player structure to have a datafile type already in the structure so i could send the struct to delete the datafile in another function.(sortof hard to explain but you'll see) but since i've done that its made loading the game take 10 times longer. it was instant before to just load the image. now it takes like 10 seconds are so now. so im wondering why this is happening. im also not sure im doing the frame limiting right or if the rest im putting to slow the animation down is the proper way of doing things. thanks a bunch for any help. i know its sortof long.

the code is attached to this topic.

LennyLen
Member #5,313
December 2004
avatar

Quote:

the code is attached to this topic.

And so is every other part of your project. If you want people to look at your code (which there's only 10Kb of), you don't need to attach ~700Kb of unnecessary data.

I'll paste the relevant part of your code here, so that others don't have to download the entire zip just to see what you are doing...

player.h:

1#ifndef _PLAYER_H
2#define _PLAYER_H
3 
4#include <allegro.h>
5#include "image.h"
6 
7struct S_Player {
8
9 int x, y;
10 int offsetx, offsety;
11 int playerspeed;
12 BITMAP* img[10];
13 int current;
14 int frame;
15 int key;
16 DATAFILE* dat;
17};
18 
19 
20struct S_Player createPlayer(void);
21void deletePlayer(struct S_Player* p);
22#endif //_PLAYER_H

player.c:

1#include "player.h"
2 
3 
4struct S_Player createPlayer(void) {
5
6 struct S_Player player;
7 player.dat = load_datafile("image/sprite.dat");
8
9 player.img[0] = (BITMAP *)player.dat[stand].dat;
10 player.img[1] = (BITMAP *)player.dat[run1].dat;
11 player.img[2] = (BITMAP *)player.dat[run2].dat;
12 player.img[3] = (BITMAP *)player.dat[run3].dat;
13 player.img[4] = (BITMAP *)player.dat[run4].dat;
14 player.img[5] = (BITMAP *)player.dat[run5].dat;
15 player.img[6] = (BITMAP *)player.dat[run6].dat;
16 player.x = 10;
17 player.y = 150;
18 player.playerspeed = 6;
19 player.current = 1;
20 player.key = 1;
21
22 return player;
23}
24 
25 
26void deletePlayer(struct S_Player* p) {
27
28 unload_datafile(p->dat);
29
30}

[edit]Oops, forgot to add the drawing routines:

game.c:

1#include "game.h"
2 
3volatile int ticks;
4 
5void startUp(void) {
6
7 allegro_init();
8 install_keyboard();
9 install_timer();
10 set_color_depth(16);
11 set_gfx_mode(GFX_AUTODETECT_FULLSCREEN,320,200,0,0);
12 LOCK_FUNCTION(ticker);
13 LOCK_VARIABLE(ticks);
14 install_int_ex(ticker, BPS_TO_TIMER(30));
15}
16 
17void ticker(void) {
18
19 ticks++;
20}
21END_OF_FUNCTION(ticker)
22 
23void setupTicker(struct S_Player* p) {
24
25 p->frame = ticks;
26}
27 
28BITMAP* createBuffer(void) {
29
30 BITMAP* buffer = create_bitmap(SCREEN_W,SCREEN_H);
31 return buffer;
32}
33
34void drawMenu(BITMAP* b, struct S_Menu* m) {
35
36 if ((key[KEY_DOWN]) && (m->current <= 3)) m->current ++;
37 if (key[KEY_UP]) m->current --;
38 if (m->current > 3) m->current = 0;
39 if (m->current < 0) m->current = 3;
40
41 rest(85);
42 draw_sprite(b,m->img[m->current],m->x,m->y);
43 blit(b,screen,0,0,0,0,SCREEN_W,SCREEN_H);
44 clear_bitmap(b);
45}
46 
47void drawScreen(BITMAP* b, struct S_Player* p) {
48
49 if (p->key == 0) draw_sprite_h_flip(b,p->img[p->current],p->x,p->y);
50 else draw_sprite(b,p->img[p->current],p->x,p->y);
51
52 rest(50);
53 while (p->frame > ticks) rest(1);
54
55 p->frame++;
56
57 blit(b,screen,0,0,0,0,SCREEN_W,SCREEN_H);
58 clear_bitmap(b);
59
60 updateInput(p); //Input
61
62}
63 
64void updateInput(struct S_Player*p) {
65
66 if (key[KEY_RIGHT]) {
67 p->key = 1;
68 p->current++;
69 if (p->current > 5) p->current = 1;
70 p->x += p->playerspeed + 4;
71
72 } else if (key[KEY_LEFT]) {
73
74 p->key = 0;
75 p->current++;
76 if (p->current > 5) p->current = 1;
77 p->x += -p->playerspeed - 4;
78
79 } else p->current = 0;
80}

dothedru22
Member #7,325
June 2006

yea. sorry. i did that so people would know how long it took to load. i would have had to post the dat files and stuff anyway if i just gave the .exe to see that. so i just figured i'd do that. but i appreciate you posting it on the forum and stuff.

A J
Member #3,025
December 2002
avatar

rest() will pause your program. It is not an effecient way to wait for events to occur.

You should be waiting based on a timer variable.
Then only, and only then when you have no work to do you call rest().

___________________________
The more you talk, the more AJ is right. - ML

dothedru22
Member #7,325
June 2006

yea i am. i did that fps limiting thats posted on these forums somewhere. im just rying to slow the animtion down.

main.c

1 
2#include "game.h"
3 
4int main() {
5
6 startUp();
7 BITMAP* buffer = createBuffer();
8 struct S_Menu menu = createMenu();
9
10 struct S_Player player = createPlayer();
11
12
13 while (!key[KEY_ESC]) {
14
15 drawScreen(buffer,&player);
16 hline(buffer,0,190,320,makecol(255,255,255));
17
18 }
19
20 destroyMenu(&menu);
21 deletePlayer(&player);
22
23 return 0;
24}
25END_OF_MAIN()

Kitty Cat
Member #2,815
October 2002
avatar

Your create* functions should allocate (via malloc) the objects, then return a pointer to them, then the destroy* functions should free (via free) them. Having objects on the stack is a bad idea, because there's limited space there.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

dothedru22
Member #7,325
June 2006

well i've never understood memory mangement that well. like the heap and stack stuff. like if i use malloc to see if theres enough memory to allocate for the structure thats all fine and dandy but what if theres not and it fails. i can't run my game? o well. whatever though i'll use malloc.

Fladimir da Gorf
Member #1,565
October 2001
avatar

Quote:

using a lot of pointers and stuff across a lot of files. it just seems like this is the slow way of doig things. like that im not gettting the most fps out of it.

Well, if you'd do that I bet speed wouldn't be your biggest issue... And the slowdown is caused by the drawing commands. Always.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

dothedru22
Member #7,325
June 2006

well before my player.c looked something like this

1struct S_Player createPlayer(void) {
2
3 DATAFILE* dat
4 struct S_Player player;
5 dat = load_datafile("image/sprite.dat");
6
7 player.img[0] = (BITMAP *)dat[stand].dat;
8 player.img[1] = (BITMAP *)dat[run1].dat;
9 player.img[2] = (BITMAP *)dat[run2].dat;
10 player.img[3] = (BITMAP *)dat[run3].dat;
11 player.img[4] = (BITMAP *)dat[run4].dat;
12 player.img[5] = (BITMAP *)dat[run5].dat;
13 player.img[6] = (BITMAP *)dat[run6].dat;
14 player.x = 10;
15 player.y = 150;
16 player.playerspeed = 6;
17 player.current = 1;
18 player.key = 1;
19
20 return player;
21}

and that loaded up fast and stuff. the player.c that was posted before takes about 10 seconds. just wondering why that is.

Go to: