#include <SFML/Graphics.hpp>
sf::Image temp;
#include <iostream>
#define MAPWIDTH 10
#define MAPHEIGHT 10
#define BLOCK_NUMBER 2
#define ENTITY_NUMBER 1
#define MESH_ARRAY_NUMBER 1
#define ITEM_NUMBER 5
#define INVENTORY_WIDTH 3
#define INVENTORY_HEIGHT 2
sf::Sprite sprite;
sf::Texture texture;
sf::Sprite player_image;
sf::Vector2i mouse_information;
bool show_inventory;
bool show_menu;
sf::RenderWindow window(sf::VideoMode(64+(MAPWIDTH*32), 64+(MAPHEIGHT*32)), "Adventure Game");
signed int current_block_temp_x;signed int current_block_temp_y;
#define FRAMES 7
/////////////////////////////////General functions////////////////////////////////
void wait(int miliseconds)
{
sf::Time elapsed;
int exit1=1;
sf::Clock clock;
while(exit1)
{
elapsed=clock.getElapsedTime();
if(elapsed.asMilliseconds()>=miliseconds)
exit1=0;
}
}
/////////////////////////////////Graphics Functions///////////////////////////
sf::Font font;
sf::Text text;
void load_font(std::string filename)
{
font.loadFromFile(filename);
}
void draw_text(std::string output, int x, int y)
{
text.setFont(font);
text.setString(output);
text.setCharacterSize(18);
text.setColor(sf::Color::Black);
text.setPosition(x,y);
window.draw(text);
}
void draw_load_image(int x, int y, std::string file, bool mask=1)
{
temp.loadFromFile(file);
if (mask)
temp.createMaskFromColor(sf::Color::White,0);
texture.loadFromImage(temp);
sprite.setTexture(texture);
sprite.setPosition(sf::Vector2f(x,y));
window.draw(sprite);
}
void load_image(std::string file, bool mask = 1)
{
temp.loadFromFile(file);
if(mask)
temp.createMaskFromColor(sf::Color::White,0);
texture.loadFromImage(temp);
sprite.setTexture(texture);
}
void draw_image(int x, int y)
{
sprite.setPosition(sf::Vector2f(x,y));
window.draw(sprite);
}
/////////////////////////////////////////Structures////////////////////////////////
struct map
{
int block[MAPWIDTH+1][MAPHEIGHT+1];
int entity[(MAPWIDTH+1)*16][(MAPHEIGHT+1)*16];
bool colition[MAPWIDTH*16][MAPHEIGHT*16];
}map;
struct entity
{
std::string name[ENTITY_NUMBER+1];
int health[ENTITY_NUMBER+1];
int algohrithm[ENTITY_NUMBER+1];
std::string image[ENTITY_NUMBER+1];
int number;
}entity;
struct block
{
std::string name[BLOCK_NUMBER+1];
int physics[BLOCK_NUMBER+1];
std::string image[BLOCK_NUMBER+1];
int number;
}block;
struct inventory
{
int selectx;
int selectz;
int inventory[INVENTORY_WIDTH][INVENTORY_HEIGHT];
}inventory;
struct item
{
std::string name[ITEM_NUMBER];
std::string image[ITEM_NUMBER];
std::string image2[ITEM_NUMBER];
int flags[ITEM_NUMBER];
bool useable[ITEM_NUMBER];
int flags2[ITEM_NUMBER];
int number;
int entity[ITEM_NUMBER];
}item;
/////////////////////////////////////////////////////////Expeirimental/////////////////////
//class mesh
//{
//public:
//bool main[16][16];
//bool get_mesh(int x,int y);
//void change_mesh(int x, int y, bool change);
//};
//class MESHARRAY
//{
//public:
//void clear_array(int array, bool clear);
//bool main[MESH_ARRAY_NUMBER][16][16];
//bool get_mesh(int array, int x,int y);
//void change_mesh(int array, int x, int y, bool change);
//};
//MESHARRAY block_mesh;
//MESHARRAY entity_mesh;
//bool MESHARRAY::get_mesh(int array ,int x, int y)
//{
//return main[array][x][y];
//}
//void MESHARRAY::change_mesh(int array, int x, int y, bool change)
//{
//main[array][x][y]=change;
//}
//void MESHARRAY::clear_array(int array, bool change)
//{
//int exit1=1;
//int x=1; int y=1;
//while (exit1)
//{
//x++;
//if(x>16)
//{
//x=1; y++;
//}
//if(y>16)
//{
//exit1=0;
//}
//else
//{
//main[array][x][y]=change;
//}
//}
//}
////////////////////////////////////////////Game Entity Creation/////////////////////////
void create_item(std::string name, std::string image, std::string image2, int flags=0, bool useable=0, int entity=1, int flags2=0)
{
item.number++;
item.image[item.number]=image;
item.image2[item.number]=image2;
item.name[item.number]= name;
item.useable[item.number]=useable;
item.flags[item.number]=flags;
item.flags2[item.number]=flags2;
item.entity[item.number]=entity;
}
void create_entity(std::string name, int hp, int algohrithm, std::string image)
{
entity.number++;
entity.name[entity.number] = name;
entity.health[entity.number] = hp;
entity.algohrithm[entity.number] = algohrithm;
entity.image[entity.number] = image;
}
void create_block(std::string name, int physics, std::string image)
{
block.number++;
block.physics[block.number]=physics;
//block_mesh.clear_array(block.number,mesh);
block.name[block.number]=name;
block.image[block.number]=image;
}
/////////////////////////////////////////////Player Functions/Structs/////////////////////////////
struct player
{
int x;
int y;
int id;
int speed;
int block_location_x;
int block_location_y;
int face;
bool mesh[16][16];
}player;
void set_player_xy(int x, int y)
{
map.entity[player.x][player.y] = 0;
map.entity[x][y] = 1;
}
////////////////////////////////////////////Global Variables/////////////////////////
//MESHARRAY playermesh;
int mouse_x;
int mouse_y;
/////////////////////////////////////////////main//////////////////////////////////////
int main()
{
inventory.selectx = 1;
entity.number=0;
item.number=0;
block.number=0;
sf::RectangleShape inventory_base(sf::Vector2f(INVENTORY_WIDTH+64,INVENTORY_HEIGHT+64));
sf::RectangleShape box(sf::Vector2f(32,32));
load_font("files/cour.tff");
create_item("Wooden Sword", "files/wood_sword.bmp" , "files/wood_sword.bmp",1,1,2);
create_entity("wood_sword", 0 , 0 ,"files/wood_sword.bmp");
inventory.inventory[1][1]=1;
inventory_base.setFillColor(sf::Color(255,255,102));
box.setFillColor(sf::Color(224,224,224));
bool animation=1;
int player_animation=1;
std::cout <<"Startup\n";
//playermesh.clear_array(1, 1);
std::cout <<"Setting Player Atributes\n";
player.speed=2;
player.x=2*16;
player.y=2*16;
player.block_location_x = 13;
player.block_location_y = 2;
current_block_temp_x=0;
current_block_temp_y=0;
player.id=1;
entity.number=1;
std::cout << "Creating Render Window\n";
sf::RenderWindow window(sf::VideoMode(64+(MAPWIDTH*32), 64+(MAPHEIGHT*32)), "Adventure Game");
///////////////////////////////////////////////////////////
int exit1 = 1;
int exit2 = 2;
std::string temp_player_file;
int x1=1;
std::cout << "Creating Blocks\n";
int y1=1;
create_block("cave",1,"files/cave.bmp");
create_block("blank_grey",0,"files/blank_grey.bmp");
std::cout << "Done\nCreating Entities\n";
create_entity("player",20,0,"files/player.bmp");
std::cout << "Done\n";
int x; int y;
std::cout << "Creating Landscape\n";
x=1; y=1;
player.face=2;
exit2=1;
x1=0;y1=0;
while(exit1)
{
if(x>MAPWIDTH)
{
x=1;y++;
}
if(y>MAPHEIGHT)
{
exit1=0;
}
else
{
if(x==10 & y==10)
map.block[x][y]=1;
//while(exit2)
//{
//if(x>16)
//{
//y1++;x=0;
//}
//if(y1>16)
//{
//exit2=0;
//}
//else
//{
//map.colition[(x*32)+x1][(y*32)+y1]=1;
//x1++;
//}
//}
else
{
map.block[x][y]=2;
}
}
x++;
}
std::cout <<"\nDone";
exit1=1;x=1;y=1;
while(exit1)
{
if(x>MAPWIDTH+1)
{
x=1;y++;
}
if(y>MAPHEIGHT+1)
{
exit1=0;
}
else
{
map.entity[x][y]=0;
x++;
}
}
exit1=1;
x=1;
y=1;
int select_x=1; int select_y=1;
int x3; int y3;
std::cout << "\nWindow Created";
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
if(show_inventory)
{
x3=1;y3=1;
window.draw(inventory_base);
exit1=1;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
{
show_inventory=0;
}
while(exit1)
{
if(x3>INVENTORY_WIDTH)
{
x3=1;y3++;
}
if(y3>INVENTORY_HEIGHT)
{
x3=1; y3=1; exit1=0;
}
else
{
if(select_x == x3 & select_y == y3)
{
box.setFillColor(sf::Color(189,189,189));
}
else
{
box.setFillColor(sf::Color(224,224,224));
}
box.setPosition(x3*32,y3*32);
window.draw(box);
if(inventory.inventory[x3][y3])
{
draw_load_image(x3*32+2, y*32.2, item.image[inventory.inventory[x3][y3]]);
}
}
}
}
else
{
x=1;
y=1;
if(player_animation>FRAMES)
{
if(animation)
{
animation=0;
}
else
{
animation=1;
}
player_animation = 1;
}
exit1=1;
wait(1000/30);
Please can anyone help.
PS: I do know about classes but I find separate structures and functions
much easier for me.
while(exit1)
{
if (x>=MAPWIDTH+1)
{
x=1; y++;
}
if (y>=MAPHEIGHT+1)
{
exit1=0;
}
else
if (map.block[x][y])
{
temp.loadFromFile(block.image[map.block[x][y]]);
temp.createMaskFromColor(sf::Color::Black);
texture.loadFromImage (temp);
sprite.setTexture(texture);
sprite.setPosition(sf::Vector2f(x*32, y*32)); // absolute position
window.draw(sprite);
}
x++;
}
exit1=1;x=1;y=1;
while(exit1)
{
if (x==MAPWIDTH+1)
{
x=1; y++;
}
if (y==MAPHEIGHT+1)
{
exit1=0;
}
else
if(map.entity[x][y]==0)
{
}
else
{
temp.loadFromFile(block.image[map.block[x][y]]);
temp.createMaskFromColor(sf::Color::Black);
texture.loadFromImage (temp);
sprite.setTexture(texture);
sprite.setPosition(sf::Vector2f(x*2,y*2));
window.draw(sprite);
}
x++;
}
switch (player.face)
{
case 2:
if(animation)
temp_player_file = "files/player_down.bmp";
else
{
temp_player_file = "files/player_down2.bmp";
}
break;
case 4:
if(animation)
temp_player_file = "files/player_left.bmp";
else
{
temp_player_file = "files/player_left2.bmp";
}
break;
case 6:
if(animation)
temp_player_file = "files/player_right.bmp";
else
{
temp_player_file = "files/player_right2.bmp";
}
break;
case 8:
if(animation)
temp_player_file = "files/player_up.bmp";
else
{
temp_player_file = "files/player_up2.bmp";
}
break;
default:
player.face = 2;
}
temp.loadFromFile(temp_player_file);
temp.createMaskFromColor(sf::Color::White,0);
texture.loadFromImage(temp);
player_image.setTexture(texture);
player_image.setPosition(sf::Vector2f(player.x*2,player.y*2));
window.draw(player_image);
mouse_information = sf::Mouse::getPosition(window);
int mouse_x = mouse_information.x; // window is a sf::Window
int mouse_y = mouse_information.y;
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
if (mouse_x>player.x*2)
{
if((player.x+player.speed)/16<MAPWIDTH)
{
//if(!map.colition[player.x+player.speed][player.y])
player_animation++;
player.x = player.x+player.speed;
player.face =6;
}
}
if(mouse_x < player.x*2)
{
if((player.x-player.speed)/16>0)
{
//if(!map.colition[player.x-player.speed][player.y])
player.x=player.x-player.speed;
player_animation++;
player.face = 4;
}
}
if(mouse_y>player.y*2)
{
if((player.speed + player.y)/16<MAPHEIGHT)
{
//if(!map.colition[player.x][player.y+player.speed])
player.y=player.y+player.speed;
player_animation++;
player.face = 8;
}
}
if(mouse_y<player.y*2)
{
if((player.y-player.speed)/16>0)
{
//if(!map.colition[player.x][player.y-player.speed])
player.y=player.y-player.speed;
player.face =2;
player_animation++;
}
}
}
//
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if((player.y +player.speed)/16<MAPHEIGHT)
{
player.y=player.y-player.speed;
player.face = 8;
}
player_animation++;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
if((player.x-player.speed)>0)
{
player.face = 4;
player.x=player.x-player.speed;
}
player_animation++;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
if((player.y-player.speed)/16>0)
{
player.face =2;
player.y=player.y+player.speed;
}
player_animation++;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard:: D))
{
if((player.x+player.speed)/16<MAPWIDTH)
{
player.face =6;
player.x = player.x+player.speed;
}
player_animation++;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
{
switch(player.face)
{
case 8:
load_image(entity.image[item.entity[inventory.selectx]]);
sprite.setRotation(180);
draw_image(player.x-(16/2)*2,player.y*2);
break;
case 4:
load_image(entity.image[item.entity[inventory.selectx]]);
sprite.setRotation(270);
draw_image(player.x-(16/2)*2,player.y*2);
break;
case 2:
draw_load_image(player.x*2,player.y+(16/2)*2,entity.image[item.entity[inventory.selectx]]);
break;
case 6:
load_image(entity.image[item.entity[inventory.selectx]]);
sprite.setRotation(90);
draw_image(player.x-(16/2)*2,player.y*2);
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::E))
{
show_inventory=1;
}
}
window.display();
}
return 0;
}