Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 13 May 2011
Offline Last Active Sep 22 2013 08:04 PM

Topics I've Started

Tile Map Editor/Collision Detection

11 September 2013 - 10:11 PM

Hey everybody!

I'm continuing in my SFML endeavors and I just recently finished a tile map editor. Given a tilesheet and the size of each tile, the program is able to take a "map" where one specific color corresponds to a tile. At the moment you have to hard code the values of each color-tile pair, but I will make it so that it can read in a text file that has the color pairings(if anyone has a better way of doing this I'd be interested).

Now my issue lies here. At the moment my map editor only takes one image and creates another one out of it. I can display this image as the background and move it accordingly when my character moves yadda yadda. However I do not really understand where the collision detection is going to fit in here.Is it possible to have this information handled within the tile map editor itself? I remember ages ago when I first started Java I created a Tile class and each Tile said whether or not it was "walkable". However I have foregone that step in this case so I am at a bit of a loss at what to do.

While reading the forums I found some people talking about using "layers". So I would draw my map in the background as usual, but at the same time have an invisible layer on top of that with which the sprites/entities interacted with. But I would like this to be automatically coupled with the tiles(e.g. grass is walkable tile, but stone is not). Also on a similar note, I am curious as to the best way of storing this "walkable" information. As some objects could become walkable later on should the user use a certain item, for example water if the user gets into a boat. I don't think this should change water's walkable property, but instead override it somehow. Again, I am lost here. 

I will include the code as an attachment as well as in the post itself for the map editor in case it is of use to the discussion.

Thanks again for the help!

Map.h and Map.cpp

#ifndef MAP_H
#define MAP_H

//SFML Includes
#include <SFML/Graphics.hpp>

//C++ Includes
#include <iostream>
#include <string>

using namespace std;

class Map
	Map();//Don't use this lol
	Map(string rawMapFile,string tileFile,int tSize);//Accepts map of pixels, tile spritesheet, and the size of each tile

	void process();//Takes map of pixels and turns it into map of tiles
	void initTileColors();//Initializes the color values for each tile
	sf::Image rawMap; //The map generated by the user where each pixel represents a tile
	sf::Image tileSpriteSheet; //The map generated by the program and saved to disk for user

	int numOfTiles;//How many tiles the user has in their spriteSheet
	int tileSize; //The size of each tile in the spriteSheet, MUST BE SQUARE

	sf::Color tileColors[];


//SFML Includes
#include <SFML/Graphics.hpp>

//Personal Includes
#include "Map.h"

//C++ Includes
#include <iostream>
#include <string>

using namespace std;



Map::Map(string rawMapFile,string tileFile,int tSize)
	//Loads the rawMap image
		cout<<"Could not load file: "<<rawMapFile<<endl;

	//Loads the map's tile spritesheet
		cout<<"Could not load file: "<<tileFile<<endl;

	//Sets the size of each tile in the spritesheet and how may tiles there are
	tileSize = tSize;

void Map::initTileColors()
	//REMINDER!!! Make this user customizable through text file or something

	tileColors[0] = sf::Color(0,0,0,255);//Ground
	tileColors[1] = sf::Color(100,100,100,255);//Stone
	tileColors[2] = sf::Color(255,255,255,255);//Sky
void Map::process()
	//Creates a blank image the size of rawMap times the size of each tile in tileSpriteSheet
	sf::Image processedMap;
	processedMap.create(rawMap.getSize().x * tileSize,rawMap.getSize().y * tileSize);

	//Takes map tile spritesheet and turns all magic purple pixels into transparent pixels

	//Loops through every pixel on the raw map
	//When a pixel matches a color in the tileColors array it copies over the appropriate tile from spritesheet
	//To the processed image
	for(int x = 0; x < rawMap.getSize().x; x++)
		for(int y = 0; y < rawMap.getSize().y; y++)
			if(rawMap.getPixel(x,y) == tileColors[0])
				processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(0,0,tileSize,tileSize),true);
			if(rawMap.getPixel(x,y) == tileColors[1])
				processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(8,0,tileSize,tileSize),true);			
			if(rawMap.getPixel(x,y) == tileColors[2])
				processedMap.copy(tileSpriteSheet,x * tileSize,y * tileSize,sf::IntRect(16,0,tileSize,tileSize),true);			


[SFML 2.1] Growing Plants

30 August 2013 - 05:56 PM

Hello everyone!

it's been a long while since I've posted. I'll keep this topic short and sweet.

I just finished C++ at school and decided to start a project. Main game mechanic revolves around growing plants and using them in combat(still have a few ideas on this floating around but that's for another post another day tongue.png ). I am in the early stages and have decided to represent the plants as rectangles and have them slowly increase their height to grow upwards.

I have created a Plant class that inherits from sf::RectangleShape. Plant has width, height, x, and y floats.When I create a new plant it will take a constructor Plant(float w, float h, float x, float y). It will then use setSize(sf::Vector2f(w,h)) for its size and then position itself using setPosition(x,y).

As it is, when I change the height of the plant using setSize(), the plant would grow downward. If I were to have the plant grow upward I would have to increase its height while also shifting it down using either setPosition() or move(). This doesn't really seem optimal but I can't really think of any better way of doing this.

Another concern is whether the variable growthRate of plants should be handled by a varying multiplier, or "tick". Perhaps better phrased, should I "grow" all the plants at the same time and they vary how much they grow( every 10 seconds plants grow a variable height y), or should I grow all plants a universal height and they vary how often they grow( every plant grows 10px but they each grow every t seconds). Any thoughts?



I was being silly and completely forgot how inheritance worked, solved it quickly, and removed that part. The part pertaining to the actual plant growth still remains though. Sorry! I was too quick to post tongue.png

OpenGL Setup Issues

10 February 2013 - 10:40 PM

Hello everybody!

I finally decided to jump in and start learning some OpenGL. I am using Code::Blocks and the MinGW compiler. I am also using freeGLUT for windowing, as well as GLEW for the handling of extensions. I am running Windows 7 and my AMD HD 6450 has it's drivers all up to date. I believe the problem I am having is specifically with GLEW. The reason I say this is because I was able to open up a window with freeGlut and everything

was fine and dandy until I tried adding in GLEW

Whenever I try building my project it throws me "undefined reference to ___" errors to several OpenGL functions. The weird part too is for example when it complains about glEnableVertexAttribArray it says:

undefined reference to `_imp___glewEnableVertexAttribArray

Now I'm not too knowledgeable to these things so I tried exercising my Google-fu. Alas my opponent was too strong and I was soundly defeated. The only possible lead I have is that the GLEW lib is not being recognized because the GLEW binaries are not compiled for MinGW, but only for Visual Studio.

Please let me know if you can come up with any solutions, I have been tearing out my hair in frustration at this issue for quite some time now.

As some added information I am following these tutorials on OpenGL codewise:

However, I used LazyFoo's tutorials to set up OpenGL on Code::Blocks.

Thank you so much for your help!


Impact of Using Outdated Resources

08 January 2013 - 09:05 PM

Hello everyone!

I recently stumbled across codeacademy as well as other free coding websites who's approach to teaching I really enjoy(more interactive and quiz based). I was just doing their HTML tutorials but soon I noticed that it was "old code". The reason I noticed this is because I had also been reading in parallel "HTML & CSS design and build websites". This book teaches you HTML and CSS while trying to push the reader in the direction of HTML5. It does this by making notes of where old code is being used and let's the reader know how it is now implemented in the new standard.

For this reason, I have no problem with going through the tutorials at codeacademy for HTML and CSS because my book will let me know which are old methods and which aren't. However, there are other areas of programming like the Python tutorials and Javascript tutorials that I am not sure about. I know for a fact that the codeacademy tutorials use Python 2.x  and that the standard is now Python 3.

Do you think it would greatly impact me if I went through those tutorials and then moved on to the newer material? If so, is there "interactive" material you could suggest for these? This applies to all programming languages and programming topics in general but at the moment the most relative topics for me would be:

-Python(I have Dive into Python 3, but no interactive material)
-Javascript(I have nothing)
-HTML & CSS (I have HTML & CSS design and build websites)
-OpenGL(I have nothing)

Thanks for the help as always!


Bitwise Confusion

25 November 2012 - 03:55 PM

Hey all!

Just had a quick question that I've been trying to debug on my own to no avail. I'm currently following a tutorial on how to manipulate an image's pixels and colors through bitwise operations but I seem to have reached a part that confounds me.

At one part he writes this line of code

pixels[i] & 0xff;

and says that this line of code will "get rid of the alpha channel" from the colors.

Now what I understand is that this "&" operator goes through the bits and performs the logical "and" operation. So if you and'ed 8 and 2 you would get:

8 ------- 1000
2 ------- 0010

5 ------- 0101

as it returns 1 if the parallel bits are the same, and 0 if they're not.

So as a test I and'ed pure opaque blue [0xFF0000FF(dec value 255)] with the supposed alpha channel [0xFFFF(dec value 255)] and continually lowered the blue all the way to zero;

The results I got were simply the color value (255,254,253,......,0) but if I increased the color value above 255 I would simply get a zero back.

[source lang="java"]for(int i = 255; i >0; i--){ System.out.println(i & 255);}[/source]

From my results I then assumed that this operation then just returned all values less than or equal to the second operand(in this case 0xFF) and returned 0 for all values greater than the second operand. However, I tried to replicate this with other numbers like 16&16/0x10&0x10 but the results did not match. With those numbers, any number that was not equal to the second operand yielded 0, and if equal yielded the second operand(16,0,0,0,0.....)
[source lang="java"] public static void loop() { for (int i = 16; i > 0; i--) { System.out.println(i&16); } }[/source]

I kept checking numbers to see when I would return to the original behavior that I observed, and noted that it doesn't return to the pattern I saw before but some numbers will give sequences like (128,128,128,128,128,0,0,0,0,0,64,64,64,0,0,0,) and such.

Thus I am completely baffled and ask for your help in this. I understand what the & operator does, but I don't understand how it's being "used" here. I will include the video link to the tutorial I'm following. The code is very similar to the code I saw Notch use on Minicraft for Ludum Dare which I'm guessing is where the person in the tutorial learned from. The rest of his videos continue on in this fashion using a lot of bitwise operations so it's crucial for me to grasp on to how he's using it here. He didn't explain very well how he uses what he uses.


Thank you again for your help!