cannot convert from "initializer list" - c++

I am new and have an issue in C++. I am creating 3D coordinates, (x, y and z for each corner of a face, then 6 faces) and receive many errors. Here is my code:
#include <vector>
int main()
{
std::vector<int> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
int x1 = 0;
int y1 = 1;
int z1 = 2;
int x2 = 3;
int y2 = 4;
int z2 = 5;
xyzCoords = {
{ x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2 },
{ x1, y2, z1, x2, y2, z1, x2, y2, z2, x1, y2, z2 },
{ x1, y2, z1, x1, y1, z1, x1, y1, z2, x1, y2, z2 },
{ x2, y2, z1, x2, y1, z1, x2, y1, z2, x2, y2, z2 },
{ x1, y2, z2, x1, y1, z2, x2, y1, z2, x2, y2, z2 },
{ x1, y2, z1, x1, y1, z1, x2, y1, z1, x2, y2, z1 }
};
return 0;
}
This the code where the problem occurs. You can see I'm defining xyzCoords as a vector. I'm not sure if this is the correct way to do it. I also don't want to be defining xyz123 separately. What is the best way to achieve this? Shall I use a list, array or vector? Please write the code for how to do this. Thanks!
Errors:
E0289: no instance of constructor "std::vector<_Ty, _Alloc>::vector [with _Ty=int, _Alloc=std::allocator]" matches the argument list
E0349: no operator "=" matches these operands
C2440: 'initializing': cannot convert from 'initializer list' to 'std::vector>'
C2679: binary '=': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion)

Your initialization is unmatch with the type you had declared. You declare a 1D array whereas your initialization is 2D array.
std::vector<int> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
It should be :
std::vector<std::vector<int>> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
You mentioned about xyz coordinate, so why don't you make a class specially used for coordinate, for instance :
struct Point {
int x, y, z;
};
Anyway, I don't recommend you to use naive approach for this problem because it will be a matrix-operation-intensive computation. You should use BLAS/LAPACK library or it's wrapper like Eigen or Armadillo. They are surely far faster.

Perhaps you should restructure your code to something like this, for visibility and better understanding:
#include <vector>
int main()
{
struct Corner
{
int x;
int y;
int z;
Corner(int x, int y, int z) :
x(x), y(y), z(z) {}
};
struct Face
{
Corner c1;
Corner c2;
Corner c3;
Corner c4;
Face(Corner c1, Corner c2, Corner c3, Corner c4) :
c1(c1), c2(c2), c3(c3), c4(c4) {}
};
Corner c1(0, 1, 2);
Corner c2(3, 4, 5);
Face f1(c1, c2, c1, c2);
std::vector<Face> faces = { f1, f1, f1, f1, f1, f1 };
return 0;
}
This creates array of 6 faces with coordinates of the corners (x1,y1,z1) (x2,y2,z2) (x1,y1,z1) (x2,y2,z2)

#include <vector>
struct Vertex {
float x_; // can change these to int
float y_;
float z_;
Vertex() : x_(0), y_(0), z_(0) {}
Vertex( float x, float y, float z ) : x_(x), y_(y), z_(z) {}
explicit Vertex( float val ) : x_(val), y_(val), z_(val) {}
};
struct Face {
Vertex v0_;
Vertex v1_;
Vertex v2_;
Vertex v3_;
Face() :
v0_(Vertex()),
v1_(Vertex()),
v2_(Vertex()),
v3_(Vertex()) {
}
Face( Vertex v0, Vertex v1, Vertex v2, Vertex v3 ) :
v0_(v0),
v1_(v1),
v2_(v2),
v3_(v3) {
}
};
int main() {
std::vector<Face> faces;
// Not exact values the OP is looking for,
// just a quick way to populate the vector of faces
// for demonstration purposes of the usefulness of structs & classes.
for ( unsigned n = 1; n <= 6; n++ ) {
faces.push_back( Face( Vertex(n), Vertex(n+1), Vertex(n+2), Vertex(n+3) ) );
}
return 0;
}

Related

Add Z dimension to 2D Homogeneous Transform Matrix

This is something I've been trying to work out for a while - I'm trying to go from a 2D x,y,w homogeneous transform matrix (eg. Android's graphics.matrix) and turn it into a 3D x,y,z,w transform matrix (eg. Android's opengl.matrix)
Convert an android.graphics.Matrix to a GL mat4? seems to be a similar question but the answer only applies to affine transformations while I need to handle perspective transformations in x and y. This also seems like something more general than a Android specific implementation.
I've tried making a system of equations to solve for what the 3D matrix values need to be such that for five points, the results for X and Y equal their value after the 2D transformation is applied and Z always equals one, but if the Z value is always left the same (what I want to happen) then the matrix is not invertible.
import numpy as np
# arbitrary points to sample
# need 5 points to have 5 * 3d = 15 equations to solve for 15 matrix coefficents
x0 = -1
y0 = 1
z0 = 1
u0 = -1
v0 = 1
w0 = 1
x1 = 1
y1 = 1
z1 = 1
u1 = 1
v1 = 1
w1 = 1
x2 = 1
y2 = -1
z2 = 1
u2 = 1
v2 = -1
w2 = 1
x3 = -1
y3 = -1
z3 = 1
u3 = -1
v3 = -1
w3 = 1
x4 = 0
y4 = 0
z4 = 1
u4 = 0
v4 = 0
w4 = 1
# The following numbers seem to allow the calculation to finish but are not what i want
# x4 = 21
# y4 = 15
# z4 = 36
# u4 = 12
# v4 = 21
# w4 = 31
# matrix made by extending https://stackoverflow.com/a/57280136 into 3d
A = np.matrix([
[x0, y0, z0, 1, 0, 0, 0, 0, 0, 0, 0, 0, -x0*u0, -y0*u0, -z0*u0],
[x1, y1, z1, 1, 0, 0, 0, 0, 0, 0, 0, 0, -x1*u1, -y1*u1, -z1*u1],
[x2, y2, z2, 1, 0, 0, 0, 0, 0, 0, 0, 0, -x2*u2, -y2*u2, -z2*u2],
[x3, y3, z3, 1, 0, 0, 0, 0, 0, 0, 0, 0, -x3*u3, -y3*u3, -z3*u3],
[x4, y4, z4, 1, 0, 0, 0, 0, 0, 0, 0, 0, -x4*u4, -y4*u4, -z4*u4],
[0, 0, 0, 0, x0, y0, z0, 1, 0, 0, 0, 0, -x0*v0, -y0*v0, -z0*v0],
[0, 0, 0, 0, x1, y1, z1, 1, 0, 0, 0, 0, -x1*v1, -y1*v1, -z1*v1],
[0, 0, 0, 0, x2, y2, z2, 1, 0, 0, 0, 0, -x2*v2, -y2*v2, -z2*v2],
[0, 0, 0, 0, x3, y3, z3, 1, 0, 0, 0, 0, -x3*v3, -y3*v3, -z3*v3],
[0, 0, 0, 0, x4, y4, z4, 1, 0, 0, 0, 0, -x4*v4, -y4*v4, -z4*v4],
[0, 0, 0, 0, 0, 0, 0, 0, x0, y0, z0, 1, -x0*w0, -y0*w0, -z0*w0],
[0, 0, 0, 0, 0, 0, 0, 0, x1, y1, z1, 1, -x1*w1, -y1*w1, -z1*w1],
[0, 0, 0, 0, 0, 0, 0, 0, x2, y2, z2, 1, -x2*w2, -y2*w2, -z2*w2],
[0, 0, 0, 0, 0, 0, 0, 0, x3, y3, z3, 1, -x3*w3, -y3*w3, -z3*w3],
[0, 0, 0, 0, 0, 0, 0, 0, x4, y4, z4, 1, -x4*w4, -y4*w4, -z4*w4]
])
print(A)
print(np.linalg.det(A)) # zero
b = np.array([u0, u1, u2, u3, u4, v0, v1, v2, v3, v4, w0, w1, w2, w3, w4])
c = np.linalg.solve(A, b) # crashes here
mat3d = np.matrix([
[c[0], c[1], c[2], c[3]],
[c[4], c[5], c[6], c[7]],
[c[8], c[9], c[10], c[11]],
[c[12], c[13], c[14], 1]
])
print(mat3d)
Is there a way to reliably extend a 2D homogeneous coordinates matrix to a 3D one?
Thanks!
If I understand your question, have a 3x3 matrix M1 and you want a 4x4 matrix M2, such that:
Wherever M1[x1,y1,w1] = [x2,y2,w2]...
You want M2[x1,y1,0,w1] = [x3,y3,z3,w3]...
Such that x2/w2 = x3/w3, y2/w2 = y3/w3, z3/w3 = 1
The easiest way is just to extend the matrix so that x3 = x2, y3 = y2, z3 = w2, w3 = w2. To do that you just add a z column that is the same as the w column, and a z row that is all zeros

C++ SFML: How to successfully Iteratively render shapes?

I'm looking to iteratively create a set of rectangle shapes of different colours and display them on the screen in different positions. This should produce a maze made up of rectangles.
The positions of the shapes should reflect the array that stores a 9x9 maze.
I'm able to successfully create a single shape, change it's colour and position. However, when I try to do the same to a shape inside an array it does not work. Only a single blue rectangle is generated in the top right corner of the screen.
int main() {
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Baphomet");
sf::RectangleShape tiles[81] = {sf::RectangleShape(sf::Vector2f(20, 20))};
char maze[81] = {0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 1, 1, 0,
0, 1, 0, 1, 0, 1, 0, 1, 0,
0, 1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 0, 1, 0, 1, 0, 1, 0,
0, 1, 1, 1, 0, 1, 1, 1, 0,
0, 0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0};
int x;
int y;
for (int i = 0; i < 81; i++) {
if (maze[i] == 0) {
tiles[i].setFillColor(sf::Color::Blue);
} else if (maze[i] == 1) {
tiles[i].setFillColor(sf::Color::Red);
}
x = (i % 9) * 20;
y = (i / 9) * 20;
std::cout << x << " " << y << std::endl;
tiles[i].setPosition((float)x, (float)y);
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (int i = 0; i < 81; i++) {
window.draw(tiles[i]);
}
window.display();
}
return 0;
}
How can I make the shapes change their positions to the x and y variables successfully?
I think the main problem is the definition
sf::RectangleShape tiles[81] = {sf::RectangleShape(sf::Vector2f(20, 20))};
Here you explicitly initialize only the first element (tiles[0]). All the rest of the elements will only have default-constructed shape objects.
To properly initialize all elements to the same values, either use a loop
for (auto& shape : tiles)
shape = sf::RectangleShape(sf::Vector2f(20, 20));
Or use a vector
std::vector<sf::RectangleShape> tiles(81, sf::RectangleShape(sf::Vector2f(20, 20)));

How to get Player to interact with Monster using ncurses library?

I've been trying to create my roguelike type of game for C++ using the ncurses library. After following many different tutorials, I am able to create the Player character, the map, the code to prevent the Player from going through walls and the random movements for the Monster character.
The next problem I'm encountering is implementing a boolean so whenever the Player character interacts with the Monster character, the game will quit (much like the roguelike games). However, I can't seem to get it to function the way I wanted. I think it has to do with the coordinates that I have set for the Player and the Monster but I'm still not sure. Can anyone help me out please?
Here is the code:
#include <iostream>
#include <ncurses.h>
#define MAP_WIDTH 22
#define MAP_HEIGHT 15
#define TILE_FLOOR 0
#define TILE_WALL 1
int PlayerX, PlayerY;
void erase (int y, int x) {
mvaddch(y, x, '.');
}
int nMapArray[MAP_HEIGHT][MAP_WIDTH] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
bool IsPassable (int nMapX, int nMapY) { //prevents from walking into walls
if (nMapX < 0 || nMapX >= MAP_WIDTH || nMapY < 0 || nMapY >= MAP_HEIGHT)
return false;
int nTileValue = nMapArray[nMapY][nMapX];
if( nTileValue == TILE_FLOOR) {
return true;
}
return false;
}
class Monster {
public:
void Appearance(char monster) {
this->Monster = monster;
}
void SetPos(int x, int y) {
this->PosX = x;
this->PosY = y;
}
void Movement(int &MonsX, int &MonsY) {
int x = (rand() % 3 - 1);
int y = (rand() % 3 - 1);
if (IsPassable(this->PosX+x, this->PosY+y)) {
erase(PosY, PosX);
MonsX = this->PosX += x;
mvaddch(this->PosY, MonsX, this->Monster);
refresh();
erase(PosY, PosX);
MonsY = this->PosY += y;
mvaddch(MonsY, this->PosX, this->Monster);
refresh();
}
}
protected:
int PosX;
int PosY;
char Monster;
};
bool MonsterContact (int nMapY, int nMapX, int x, int y) {
if (nMapArray[nMapY][nMapX] == nMapArray[y][x]) {
return true;
}
return false;
}
void map() {
for (int y = 0; y < MAP_HEIGHT; y++) { //loops to print the map
move(y,0);
for (int x = 0; x < MAP_WIDTH; x++) {
switch (nMapArray[y][x]) {
case TILE_FLOOR:
printw(".");
break;
case TILE_WALL:
printw("#");
break;
}
}
}
};
void init() { //starts the ncurses screen.
initscr();
clear();
noecho();
raw();
keypad(stdscr, TRUE);
curs_set(0);
}
void game_loop (char Player, int row, int col, int ch) {
Monster npc;
npc.SetPos(7, 8);
npc.Appearance('g');
int MonsX,MonsY;
mvaddch(row,col, Player); //player movement
refresh();
while(true) {
npc.Movement(MonsX, MonsY);
ch = getch();
switch (ch) {
case 'w':
if (IsPassable(col, row-1)) {
erase(row,col);
row = row - 1;
mvaddch(row, col, Player);
refresh();
}
if (MonsterContact(col, row, MonsX, MonsY)) {
return();
}
break;
case 's':
if (IsPassable(col, row+1)) {
erase(row, col);
row = row + 1;
mvaddch(row, col, Player);
refresh();
}
if (MonsterContact(col, row, MonsX, MonsY)) {
return();
}
break;
case 'a':
if (IsPassable(col-1, row)) {
erase(row,col);
col = col - 1;
mvaddch(row, col, Player);
refresh();
}
if (MonsterContact(col, row, MonsX, MonsY)) {
return();
}
break;
case 'd':
if (IsPassable(col+1, row)) {
erase(row,col);
col = col + 1;
mvaddch(row,col, Player);
refresh();
}
if (MonsterContact(col, row, MonsX, MonsY)) {
return();
}
break;
case 'q':
return;
default:
break;
}
}
}
int main(int argc, const char * argv[]) {
PlayerX = 2, PlayerY = 1; //Player initial position.
char Player = '#';
init(); //starts the ncurses screen.
printw("Press any key to start the game");
int ch = getch();
clear();
map();
game_loop(Player, PlayerY, PlayerX, ch);
endwin();
return 0;
}
I'll sum up what jonhopkins commented on.
This is essentially a result of inconsistency. Your code passes arguments in different orders in different functions (first x, then y in isPassable, but vice versa in MonsterContact), and uses different names for the same things (row and x are the same.)
Your problem is caused by the fact that you passed col, row into MonsterContact, when you should have passed row, col. Perhaps you subconsciously copied the order of arguments from when you wrote isPassable a little earlier, forgetting that the order of arguments is reversed. Or you momentarily mistakenly thought that col meant y and row meant x.
Always remember to keep your code as consistent as possible, and you can avoid these sorts of mistakes in the future.

How can I rotate an image about the yaxis with opencv?

So I'm trying to place an image into a scene using opencv. I can rotate it about its centre (z axis) using GetRotationMatrix2d and warpaffine. I'm wondering to I need to use a perspective transform? Or can I just create a 3d rotation matrix and plug it into warpaffine? And is there any getRotationMatrix equivalent for 3d?
Thanks!
Edit: I found this post but it does not work for me.
My code is below And I have gotten it to rotate, although I'm not sure what focal length I should be using? everytime I rotate about a different angle I need to play around with the focal length to get it right. Say if I rotate my image 45' it looks like the pic below but that's clearly warped off in some way...
void rotateImage(const Mat &input, Mat &output, double alpha, double beta, double gamma, double dx, double dy, double dz, double f)
{
alpha = (alpha - 90.)*CV_PI/180.;
beta = (beta - 90.)*CV_PI/180.;
gamma = (gamma - 90.)*CV_PI/180.;
// get width and height for ease of use in matrices
double w = (double)input.cols;
double h = (double)input.rows;
// Projection 2D -> 3D matrix
Mat A1 = (Mat_<double>(4,3) <<
1, 0, -w/2,
0, 1, -h/2,
0, 0, 0,
0, 0, 1);
// Rotation matrices around the X, Y, and Z axis
Mat RX = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, cos(alpha), -sin(alpha), 0,
0, sin(alpha), cos(alpha), 0,
0, 0, 0, 1);
Mat RY = (Mat_<double>(4, 4) <<
cos(beta), 0, -sin(beta), 0,
0, 1, 0, 0,
sin(beta), 0, cos(beta), 0,
0, 0, 0, 1);
Mat RZ = (Mat_<double>(4, 4) <<
cos(gamma), -sin(gamma), 0, 0,
sin(gamma), cos(gamma), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
// Composed rotation matrix with (RX, RY, RZ)
Mat R = RX * RY * RZ;
// Translation matrix
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, dx,
0, 1, 0, dy,
0, 0, 1, dz,
0, 0, 0, 1);
// 3D -> 2D matrix
Mat A2 = (Mat_<double>(3,4) <<
f, 0, w/2, 0,
0, f, h/2, 0,
0, 0, 1, 0);
// Final transformation matrix
Mat trans = A2 * (T * (R * A1));
// Apply matrix transformation
warpPerspective(input, output, trans, input.size(), INTER_LANCZOS4);
}
Does translation work? Try the following code instead.
Mat T = (Mat_<double>(4, 4) <<
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
dx, dy, dz, 1);

C++ Compiler Error 2086 redefinition

I have a .cpp file that must include Console.h. In the file I'm trying to create a map (used later on for a game).
Error C2086: 'int nMapArray[15][20]: redefinition
#include "Console.h"
#include <Windows.h>
#include <stdint.h>
// Map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 15
// Tile Types
#define TILE_FLOOR 0
#define TILE_WALL 1
// Map declaration
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ];
// Map Layout
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]=
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
I know that I'm only supposed to declare nMapArray once but I'm not sure which one to discard. If I discard int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]; then it will generate two errors:
LNK2019: unresolved external symbol
and
LNK1120: unresolved externals
Did a bit of googling but I still can't find the fix so help would be appreciated.
EDIT:
Ok so following the advice of many to get rid of the first inisialisation. From here I get two errors:
error LNK2019: unresolved external symbol "public: virtual class IConsole & __thiscall Win32Console::Color(unsigned short)" (?Color#Win32Console##UAEAAVIConsole##G#Z) referenced in function "void __cdecl DrawTile(int,int)" (?DrawTile##YAXHH#Z)
and
error LNK1120: 1 unresolved externals
Full code:
#include "Console.h"
#include <Windows.h>
#include <stdint.h>
// Map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 15
// Tile Types
#define TILE_FLOOR 0
#define TILE_WALL 1
// Map Layout
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]=
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
void DrawMap( void );
bool IsPassable( int x, int y );
void DrawTile( int x, int y );
int main( void )
{
console.SetTitle( "Article Two Demo" );
// Declare the player's position
int nPlayerX = 4;
int nPlayerY = 4;
// Main game loop
while( true )
{
// Draw the map
DrawMap();
// Draw the player to the screen
console.Color( RED );
console.Position( nPlayerX, nPlayerY );
console << '#';
// Input phase - Wait for the player to do something
KEYPRESS sKeyPress = console.WaitForKeypress();
// Process the input
switch( sKeyPress.eCode )
{
// Move up
case CONSOLE_KEY_UP:
// Can we move to the tile above?
if( IsPassable(nPlayerX, nPlayerY-1) )
{
// Move up
nPlayerY--;
}
break;
// Move left
case CONSOLE_KEY_LEFT:
// Can we move to the tile to the left of the player?
if( IsPassable(nPlayerX-1, nPlayerY) )
{
// Move left
nPlayerX--;
}
break;
// Move right
case CONSOLE_KEY_RIGHT:
// Can we move to the tile to the right of the player
if( IsPassable(nPlayerX+1, nPlayerY ) )
{
// Move right
nPlayerX++;
}
break;
// Move down
case CONSOLE_KEY_DOWN:
// Can we move to the tile below the player?
if( IsPassable(nPlayerX, nPlayerY+1) )
{
// Move down
nPlayerY++;
}
break;
// Escape key
case CONSOLE_KEY_ESCAPE:
// Quit the program
return 0;
// Ignore any other keys
default:
break;
}
}
// If execution gets here, the program is done
return 0;
}
// IsPassable Function ///////////////////////////////////////////////////////////////////
//
// This function analyzes the coordinates of the map array specified and returns
// true if the coordinate is passable (able for the player to occupy), false if not.
//
bool IsPassable( int x, int y )
{
// Before we do anything, make darn sure that the coordinates are valid
if( x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT )
return false;
// Store the value of the tile specified
int nTileValue = nMapArray[y][x];
// Return true if it's passable
if( nTileValue == TILE_FLOOR)
return true;
return false;
}
// DrawMap Function //////////////////////////////////////////////////////////////////////
//
// This function draws the entire map to the screen.
//
void DrawMap( void )
{
for( int y = 0; y < MAP_HEIGHT; y++ )
{
for( int x = 0; x < MAP_WIDTH; x++ )
{
DrawTile(x, y);
}
}
}
// DrawTile Function /////////////////////////////////////////////////////////////////////
//
// Draws a map tile for the map coordinates specified.
//
void DrawTile( int x, int y )
{
console.Position( x, y );
switch( nMapArray[y][x] )
{
case TILE_FLOOR:
console.Color( GREY );
console << '.';
break;
case TILE_WALL:
console.Color( GREY );
console << '#';
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
In Console.h I haven't put the layout in (because I'm not entirely sure how to do that).
Get rid of the first one, the one without the initialisation.
That solves the compiler issue, the linker errors are another matter. The reason they appear is simply because the compilation phase is working once the double declaration is fixed.
Then I suggest you post another question with more details on the linker problems.