1 line of code doesn't work but compile find

I made another class function to add tick and it works fine. it does not work here but the call function does. did i miss something.

my vector<Core> sCore; is in my class under private
1
2
3
4
5
6
7
8
9
10
11
12
13
void cProcess::Process ( unsigned int mTick ) {
	if (sCore.size() > 0) {
		for (unsigned int it = 0; it < sCore.size(); it++ ) {
			if ( mTick >= sCore[it].ntick ) {
				// just don't know why this doesn,t work here
				// the one line below.
				sCore[it].ntick += sCore[it].tick;
				sCore[it].Function(pStart); 
			}
			else { sCore[it].Function(pSleep); }
		}
	}
}
Perhaps use iterators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void cProcess::Process (const unsigned int& mTick)
{
    if (!sCore.empty())
    {
         std::vector<Core>::iterator it;
         for (it = sCore.begin(); it <sCore.end(); it++)
         {
              if (mTick >= (*it).ntick)
              {
                   (*it).ntick += (*it).tick;
                   (*it).Function(pStart);
              }
              else
              {
                    (*it).Function(pSleep);
              }
         }
    }
}


I am assuming Core is defined something similar to
1
2
3
4
5
6
7
class Core
{
public: 
    void Function(int * value);
    int ntick;
    int tick;
}


And that pSleep and pStart are member pointers of cProcess.
Last edited on
got the same results as mine.

it does not add it to it.
(*it).ntick += (*it).tick;
i set it to 1000 and it never changes

but it will work here . just curious why.
1
2
3
4
5
6
7
8
unsigned int cProcess::nT (std::string id) 
{
	int idd;
	idd = cProcess::Id(id);
	//sCore[idd].ntick += sCore[idd].tick;
	if (idd > -1) { return sCore[idd].ntick; }
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum Purpose { pStop = 0 , pStart , pSleep };

struct Core {
	void (*Function)(Purpose P);
	std::string Id;
	unsigned int tick;
	unsigned int ntick;
	
	Core() {
		Function = NULL;
		Id = "";
		tick = 0;
		ntick = 0;
	}
};
Last edited on
Please post the header definition of your Core class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef ENGINECORE_H
#define ENGINECORE_H

#include <vector>
#include <string>
enum Purpose { pStop = 0 , pStart , pSleep };

struct Core {
	void (*Function)(Purpose P);
	std::string Id;
	unsigned int tick;
	unsigned int ntick;
	
	Core() {
		Function = NULL;
		Id = "";
		tick = 0;
		ntick = 0;
	}
};

class cProcess 
 {
 	private:
		std::vector<Core> sCore;
	public:
		cProcess();
		~cProcess();
		void Add (void (*Function)(Purpose P), std::string id );
		void Pop( std::string id );
		void PopAll();
		void Process ( unsigned int mTick );
		unsigned int Tickcount (); 
		void Stop( std::string id);
		void Settick( std::string id , unsigned int tick , unsigned int ntick );
		int Id (std::string id);
		void nTime ( std::string id );
		unsigned int nT (std::string id);
};


#endif 
Are you sure the values for tick and ntick are being set after they are added to the vector? I ask because I don't see a copy constructor or operator = overload in the class.

Perhaps change the Core to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Core 
{
     void (*Function)(Purpose P);
     std::string Id;
     unsigned int tick;
     unsigned int ntick;
	
     Core(void (*Function)(Purpose P) in_f = NULL, const std::string&  in_id = "", 
             const unsigned int & in_tick = 0, const unsigned int & in_ntick = 0) : 
         Function(in_f), Id(in_id), tick(in_tick), ntick(in_ntick)
    {
    }
    
    Core(const Core& in_core) :
          Function(in_core.Function), Id(in_core.Id), tick(in_core.tick), ntick(in_core.ntick)
    {
    }
     
};


Last edited on

my header file is above,
but change nTime to Tick

enginecore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "enginecore.h"

cProcess::cProcess(){}
cProcess::~cProcess(){}
		
void cProcess::Add (void (*Function)(Purpose P), std::string id ){
	int idd;
	idd = cProcess::Id(id);
	if ( idd == -1 ) {
		Core tmp;
		tmp.Function = Function;
		tmp.Id = id;
		sCore.push_back( tmp );		
	}
}
void cProcess::Pop( std::string id ) { 
	int idd;
	idd = cProcess::Id(id);
	if (idd > -1) { sCore.erase(sCore.begin()+idd); }
}
void cProcess::PopAll() { sCore.erase(sCore.begin(),sCore.end()); }
// Process all active ones
void cProcess::Process ( unsigned int mTick ) {
	if (sCore.size() > 0) 
	{
		std::vector<Core>::iterator it;
		for (it = sCore.begin(); it < sCore.end(); it++ ) 
		{
			if ( mTick >= (*it).ntick ) 
			{
				if ((*it).tick > 0) { (*it).ntick += (*it).tick; }
				(*it).Function(pStart); 
			}
			else { (*it).Function(pSleep); }
		}
	}
}
// Tickcount reports the next lowest next tick to decide delay
unsigned int cProcess::Tickcount () {
	unsigned int nxTick = 0;
	if (sCore.size() > 0) {
		for (unsigned int it = 0; it < sCore.size(); it++ ) {
			if ( nxTick == 0 ) { nxTick = sCore[it].ntick; }
			else if ( sCore[it].ntick < nxTick ) { nxTick = sCore[it].ntick; }
		}
	}
	return nxTick;
}
// Pop it there and do cleanup
void cProcess::Stop( std::string id) {
	int idd;
	idd = cProcess::Id(id);
	if ( idd > -1) { sCore[idd].Function(pStop);  }
}
void cProcess::Settick( std::string id , unsigned int tick , unsigned int ntick ) {
	int idd;
	idd = cProcess::Id(id);
	if ( idd > -1 ) { sCore[idd].tick = tick; sCore[idd].ntick = ntick; }
}
//use to check to see if it still running
int cProcess::Id (std::string id) {
	int idx = -1;
	for (unsigned int it = 0; it < sCore.size(); it++ ) {
		if (sCore[it].Id == id) { 
			idx = it;
			break;
		}
	}
	return idx;
}

void cProcess::Tick ( std::string id )
{
	int idd;
	idd = cProcess::Id(id);
	sCore[idd].ntick += sCore[idd].tick;
}
// using this to do sleepmode and debug
unsigned int cProcess::nT (std::string id) 
{
	int idd;
	idd = cProcess::Id(id);
	//sCore[idd].ntick += sCore[idd].tick;
	if (idd > -1) { return sCore[idd].ntick; }
	return 0;
}


my engine.h
1
2
3
4
5
6
7
8
9
#ifndef ENGINE_H
#define ENGINE_H

#include "enginecore.h"
#include "SDL/SDL.h"

void enginecontrol( cProcess process );

#endif 


my engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "engine.h"

void enginecontrol( cProcess process ) {
	static unsigned int sTick , nTick ;
	unsigned int cTick = 0;
	
	process.Process ( sTick );
	
	nTick = process.Tickcount();
	sTick = SDL_GetTicks();
	if (nTick == 0) { cTick = 15; }
	else if (nTick > sTick) { cTick = nTick - sTick; }
	else { cTick = 0; }
	
	if  (cTick > 10) { SDL_Delay( 10 ); }
	else if ( cTick > 0) { SDL_Delay( cTick ); }
}


mytestfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "engine.h"
#include "SDL/SDL_image.h" // add -lSDL_image to compile
#include "SDL/SDL_ttf.h" // add -lSDL_ttf
#include <sstream>

cProcess gamex;

struct Globs {
	SDL_Surface *screen , *grass , *text , *text2 , *ntext;
	SDL_Rect rscreen , rgrass , rtext , rtext2 , rntext;
	SDL_Event event;
	TTF_Font *font;

	bool gameover;
	
	Globs () {
		gameover = false;
	}
} glob;

struct fpsGlob {
	int tick;
	int runtick;
	
	fpsGlob() {
		tick = 0;
		runtick = 0;
	}
} fpsglob;

SDL_Surface *Image ( std::string file ) {
	SDL_Surface *temp = NULL, *image = NULL;
	temp = IMG_Load ( file.c_str() );
	if ( temp != NULL ) {
		image = SDL_DisplayFormat ( temp );
		SDL_FreeSurface ( temp );
	}
	
	return image;
}
SDL_Rect Rect( int x, int y ) {
	SDL_Rect rect;
	rect.x = x;
	rect.y = y;
	
	return rect;
}
SDL_Rect Rect( int x, int y , int w , int h ) {
	SDL_Rect rect;
	rect.x = x;
	rect.y = y;
	rect.h = h;
	rect.w = w;
	
	return rect;
}
// change an int to string
std::string itos( int n ) {
	std::ostringstream itos;
	itos.str("");
	itos << n;
	return itos.str();
}

SDL_Surface *rendertext ( char x[] ) {
	SDL_Surface *rtext;
	SDL_Color fcolor = { 255 ,255 ,255 };
	rtext = TTF_RenderText_Solid(glob.font , x , fcolor);
	return rtext;
}

void build () {
	char txt[] = "-1";
	SDL_Init ( SDL_INIT_EVERYTHING );
	TTF_Init();
	
	glob.screen = SDL_SetVideoMode ( 800 , 600 , 32 , SDL_SWSURFACE );
	//if ( screen == NULL ) { return 1; }
	SDL_WM_SetCaption ( "Sdl Test", NULL );
	
	glob.font = TTF_OpenFont("/usr/share/fonts/truetype/freefont/FreeSerif.ttf", 16);
	if (glob.font == NULL) { printf ("\nbadfont\n"); }
	
	glob.rtext = Rect(50,300);
	glob.rtext2 = Rect(50,330);
	glob.rntext = Rect(50,360);
	glob.rgrass = Rect(50,50);
	glob.rscreen = Rect(0,0,800,600);
	
	glob.grass = Image( "grass.bmp" );
	glob.text = rendertext ( txt );
	glob.text2 = rendertext ( txt );
	glob.ntext = rendertext ( txt );
	
}
void gameloop( Purpose P ) {
	
	switch (P) {
		case pStop:
		
		case pStart: {
			if ( glob.gameover == false ) {
				if (SDL_PollEvent ( &glob.event ) ) {
					switch ( glob.event.type ) {
						case SDL_QUIT:
				  			glob.gameover = true;
				  			break;
						case SDL_KEYDOWN:
				 			if ( glob.event.key.keysym.sym == SDLK_ESCAPE ||
				       		glob.event.key.keysym.sym == SDLK_q) {
				  				glob.gameover = true;
				  				break;
				  			} 
				  		break;
			  		}
		  		}
		  		//blit to screen here
		  		SDL_FillRect ( glob.screen , &glob.rscreen , 0 );
				SDL_BlitSurface(  glob.grass , NULL , glob.screen , &glob.rgrass );
				SDL_BlitSurface(  glob.text , NULL , glob.screen , &glob.rtext );
				SDL_BlitSurface(  glob.text2 , NULL , glob.screen , &glob.rtext2 );
				SDL_BlitSurface(  glob.ntext , NULL , glob.screen , &glob.rntext );
				SDL_Flip ( glob.screen ) ;
			}
			break;
		}
		case pSleep: {
			printf (" error ");
			if (P == pSleep) { printf(" sleep "); }
			break;
		}
		
	}
}
void fps( Purpose P ) {
	switch (P) {
		case pStop: {
			printf ( "%d\n",fpsglob.tick);
			break;
		}
		case pStart: { 
			fpsglob.runtick++;
			std::string tex;
			char txt[10];
			tex = itos ( fpsglob.tick );
			strcpy(txt , tex.c_str());
			glob.text = rendertext ( txt );
			
			tex = itos ( fpsglob.runtick );
			strcpy(txt , tex.c_str());
			glob.text2 = rendertext ( txt );
			
			tex = itos ( gamex.nT("FPS") );
			strcpy(txt , tex.c_str());
			glob.ntext = rendertext ( txt );
			
			fpsglob.tick = 0;
			break;
			}
		
		case pSleep: {
			fpsglob.tick++;
			break;
		}
	}
}
			
// game start
int main ( int argc , char* args[] ) {
	build();
	gamex.Add( gameloop, "Main" );
	gamex.Add( fps , "FPS" );
	gamex.Settick ( "FPS" , 1000 , 1000 );
	//game loop
	while ( !glob.gameover ) {
		enginecontrol ( gamex );
		
	}
	gamex.Stop ( "FPS" );
	// clean up
	SDL_FreeSurface ( glob.screen );
	SDL_FreeSurface ( glob.grass );
	SDL_FreeSurface ( glob.text );
		
	TTF_CloseFont( glob.font );
	TTF_Quit();
	SDL_Quit();
	return 0;
}
Last edited on
Did you try the copy constructor for Core?
wouldn't compile
g++ -Wall -c "enginecore.cpp" 
In file included from enginecore.cpp:1:
enginecore.h:22: error: expected ',' or '...' before 'in_f'
enginecore.h: In constructor 'Core::Core(void (*)(Purpose))':
enginecore.h:24: error: 'in_f' was not declared in this scope
enginecore.h:24: error: 'in_id' was not declared in this scope
enginecore.h:24: error: 'in_tick' was not declared in this scope
enginecore.h:24: error: 'in_ntick' was not declared in this scope
enginecore.cpp: In member function 'void cProcess::Add(void (*)(Purpose), std::string)':
enginecore.cpp: In member function 'void cProcess::Add(void (*)(Purpose), std::string)':
enginecore.h:28: note: candidates are: Core::Core(const Core&)
enginecore.h:23: note:                 Core::Core(void (*)(Purpose))
Compilation failed.

Last edited on
My mistake...

1
2
Core(void (*Function)(Purpose P) in_f = NULL, const std::string&  in_id = "", 
             const unsigned int & in_tick = 0, const unsigned int & in_ntick = 0) :


Should read:
1
2
Core(void (*in_f)(Purpose P) = NULL, const std::string&  in_id = "", 
             const unsigned int & in_tick = 0, const unsigned int & in_ntick = 0) :


Or there abouts.
it compile but it still has same error.
Maybe I'm missing it, but I don't see that your setting those values after they are added to the vector. And so your failing your logical test cases?
i thought vector hold the value. But i don't understand why it don't work in the cProcess::Process.

When i call this function in my enginecore.cpp it works.
1
2
3
4
5
6
void cProcess::Tick ( std::string id )
{
	int idd;
	idd = cProcess::Id(id);
	sCore[idd].ntick += sCore[idd].tick;
}


in my test file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
void fps( Purpose P ) {
	switch (P) {
		case pStop: {
			printf ( "%d\n",fpsglob.tick);
			break;
		}
		case pStart: { 
			gamex.Tick ( "FPS" ) // <-- call it here and it works
			fpsglob.runtick++;
			std::string tex;
			char txt[10];
			tex = itos ( fpsglob.tick );
			strcpy(txt , tex.c_str());
			glob.text = rendertext ( txt );
			
			tex = itos ( fpsglob.runtick );
			strcpy(txt , tex.c_str());
			glob.text2 = rendertext ( txt );
			
			tex = itos ( gamex.nT("FPS") );
			strcpy(txt , tex.c_str());
			glob.ntext = rendertext ( txt );
			
			fpsglob.tick = 0;
			break;
			}
		
		case pSleep: {
			fpsglob.tick++;
			break;
		}
	}
}

// game start
int main ( int argc , char* args[] ) {
	build();
	gamex.Add( gameloop, "Main" );
	gamex.Add( fps , "FPS" );
	gamex.Settick ( "FPS" , 1000 , 1000 ); // <-- where i set it
	//game loop
	while ( !glob.gameover ) {
		enginecontrol ( gamex );
		
	}
	gamex.Stop ( "FPS" );
	// clean up
	SDL_FreeSurface ( glob.screen );
	SDL_FreeSurface ( glob.grass );
	SDL_FreeSurface ( glob.text );
		
	TTF_CloseFont( glob.font );
	TTF_Quit();
	SDL_Quit();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.