Vector problems

Hi, I'm creating a title screen for my game.
The title screen will include 3 fields, set up as such:
START GAME
HELP & OPTIONS
EXIT GAME

Now, there is one selected and the rest are not selected.
The selected field will blink, which I already have working.

The problem comes down to the fact that when I push button down, it should deselect the one currently selected and select the one underneath.

Title.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #pragma once

#include "State.h"
#include "Field.h"

class CTitle :
	public CState
{
public:
	CTitle();
	~CTitle();

	void logic(int &nextState, Keyboard &keyboard);
	void update();
	void render();

private:
	int x;
	double starttime;
	ALLEGRO_FONT* font;
	std::vector<CField> fields;
};


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
#include "Title.h"

CTitle::CTitle() {
	starttime = al_get_time();
	x = 0;
	font = al_load_ttf_font("arial.ttf", 26, 0);

	fields.push_back(CField(320, 330, 1, "START GAME"));
	fields.push_back(CField(320, 360, 0, "HELP & OPTIONS"));
	fields.push_back(CField(320, 390, 0, "EXIT GAME"));
}

CTitle::~CTitle() {
}

void CTitle::logic(int &nextState, Keyboard &keyboard) {
	for (unsigned int i = 0; i < fields.size(); i++) {
		fields[i].logic();
	}

	if (keyboard.isKeyDown(ALLEGRO_KEY_DOWN)) {
		fields.at(x) =  // NOT SURE WHAT TO DO FROM THIS POINT...
	}
}

void CTitle::update() {
	for (unsigned int i = 0; i < fields.size(); i++) {
		fields[i].update();
	}
}

void CTitle::render() {
	al_clear_to_color(al_map_rgb(0, 0, 0));

	for (unsigned int i = 0; i < fields.size(); i++) {
		fields[i].render();
	}

	al_flip_display();
}


Now I'm not even sure if this is the correct of doing it, maybe there are better solutions than vectors.
Ideas and solutions are greatly appreciated.
Last edited on
Conceptually, at line 22 you want to do something like:
1
2
3
4
5
int oldX = x;
x = (x+1) % fields.size();  // move down, wrap to beginning if necessary
// tell fields[oldX] not to blink
// tell fields[x] to blink
update();

Works, almost like a charm. There's one problem where it kind of bugs when moving down/up, where it can skip one of the fields, and not move at all, but that should be an easy fix of, I think it lies somewhere in the line: (x+1)%fields.size(); if I'm not mistaken?
No, my guess is that it's this line:
if (keyboard.isKeyDown(ALLEGRO_KEY_DOWN)) {
What's to prevent this code from running twice (or 3 times or 3,000 times) when they press the key?
Using an if statement like that to check whether a key is currently down is good for movement and other things that should happen repeatedly for as long as the button is held down, but if you want something to happen once, no matter how long the key is pressed, you better use events.
You are correct, fixed this last night too.
Thank you for your help, it's much appreciated <3
Topic archived. No new replies allowed.