Help with variables.

Need help with initialization of a variable. I tried to initialize the frames variable but when I go to debug it, it just gives me 30 errors compared to the one error when I don't initialize the frames variable

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
/** Main.cpp controls lib.h for a simple
*  Gfx example and Audio example.
*/

#include "Gfx.h" // general gfx lib I made for SDL
#include "Input.h"
#include "General.h"
#include "Sprite.h"
#define screenw 620
#define screenh 480

/** CHARS ARE DONE */
//int mousex; int mousey; int mouseon = 0;
//int red = 0; int green = 0; int blue = 0;

int main(int argc, char** argv) {

	SDL_Texture* s1Texture = NULL;
	SDL_Texture* s1TextureShift = NULL;
	SDL_Rect s1Rect = { 0, 0, 18, 24 }; //player x y w h
	SDL_Texture* e1Texture = NULL;
	SDL_Rect e1Rect = { 310, 240, 16, 16 }; //enemy x y w h

	Input *input = new Input();
	Sprite *sprite = new Sprite();
	Gfx *gfx = new Gfx();
	General *general = new General();

	float frameEndTime;
	float frameStartTime;
	float frames;
	float fps;

	gfx->gfxinit(screenw, screenh); // open window with 620x480 res. with 8 bit color

	s1Texture = gfx->createTexture("gfx/s1.bmp", 0);
	s1TextureShift = gfx->createTexture("gfx/s1sneek.bmp", 1);
	e1Texture = gfx->createTexture("gfx/e1.bmp", 0);

	/* program main loop */
	while (!input->isGameDone()) {
		// get time when frame starts
		frameStartTime = SDL_GetTicks();

		input->inputLoop();
		sprite->move(input, s1Rect, e1Rect);
		general->checkCollision(input, s1Rect, e1Rect, screenw, screenh);

		gfx->clearScreen(100, 0, 0);

		if (!input->isShiftPressed()) {
			gfx->drawTexture(s1Texture, s1Rect);
		}
		else {
			gfx->drawTexture(s1TextureShift, s1Rect);
		}

		gfx->drawTexture(e1Texture, e1Rect);

		gfx->update();

		// get time at the end of frame after updating
		frameEndTime = SDL_GetTicks();
		// increment frames
		++frames;
		fps = frames / (SDL_GetTicks() / 1000);

		if ((1000 / 60) < (frameEndTime - frameStartTime)) {
			SDL_Delay((frameEndTime - frameStartTime) - (1000 / 60));
		}
	}

	delete sprite;
	delete input;
	delete general;

	gfx->close();

	delete gfx;

	return 0; // return success!
}
You haven't shown us any code initialising frames. Without seeing how you're trying to do it - or the error messages that you got when you tried it - how can we possibly even begin to know what you did wrong?

EDIT: I do have to wonder why you're declaring it as a float, when you only seem to modify it by 1 in each iteration of your loop.
Last edited on
error C4700: uninitialized local variable 'frames' used main.cpp line:65 columm:1 project:Game

thats the error

also, i changed the frame, fps, frameEndTime, and frameStartTime from float to int
all i want to know is how to initialize the variable, that's all
You initialise it by assigning it a value.

As I said:

Without seeing how you're trying to do it [...] how can we possibly even begin to know what you did wrong?
Topic archived. No new replies allowed.