C++ :: 2D Game - How To Handle Drawing Order Of Object Visibility
Feb 9, 2015
In 2D games, what's the best way to handle the order of drawing objects? Because most games have a background, tiles to be drawn behind the player, perhaps tiles to be drawn covering up the player, etc. My point is, with my current setup of simply looping through all objects and drawing, I have no control over what objects are drawn on top of or behind the others. My best idea so far is to hold a vector of object pointers, each vector representing a different "visibility level", like so:
class Level{
//...
std::vector<Object> allObjectInstances;
std::vector<Object*> visibilityOne; //background objects
std::vector<Object*> visibilityTwo; //objects in front of background but not necessarily all the way in front
//and so on for more objects
};
If I go through with this, I'm wondering how I could loop through all my objects and add them to each vector, then shorten whatever I have to loop through for subsequent visibility vectors. handling the order of drawing objects?
View 6 Replies
ADVERTISEMENT
Oct 24, 2014
I programming currently something with OpenGL. Now I have written some wrapper classes, like Shader, Program .... All works fine, but I don't like to call Shader.GetHandle() every time I want to call a OpenGL function manually where I need the object handle/id. (GetHandle() returns the OpenGL ID of the object)
So now I wonder, is it possible to program it in C++ so, that I can put my objects to gl methods and c++ automatically pass the handle/id member to that function ? Is there maybe a operator that I can use for that?
For example:
The way I do it now:
Shader vertexShader();
glCompileShader(vertexShader.GetHandle());
The way I want to:
Shader vertexShader;
glCompileShader(vertexShader);
View 3 Replies
View Related
Oct 2, 2013
A link list problem, i need some how to sort and print out an object by surname in alphabetical order.
i have a try to do that by this code not working for swamping i should i go about that?
May another way of swapping using pointer instead?
void functions::printdata() {
for(node* temp1=head;temp1!=NULL;temp1=temp1->next) {
for(node* temp2=head;temp2!=NULL;temp2=temp2->next) {
[Code]...
View 1 Replies
View Related
May 28, 2013
Been trying to figure out why my program freezes. I know exactly what line of code is causing it, but I can't figure out WHY it's causing it. It compiles fine, there are no errors returned, and then the game just stalls and I have to ctrl+alt+del to kill it.
Anyway, what I have set up is something like this:
main.cpp
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include "SDL_image.h"
#include "SDL_rotozoom.h"
[Code]....
This is the line of code that's freezing the program. Simply put, so that you don't really have to go through it piece by piece, what I have done is:
*each player gets their own drawing surface called PlayScreen
*each player gets an array of 360 drawing surfaces called ShipPic. These are to keep the game from having to render the rotation pics of the ship on the fly.
*Get_Ship clips the requested ship picture out of the ship sprite sheet and puts it in ShipPic[0] for the original angle.
*the original picture is rotated by 1 degree and put into the 360 ShipPic slots.
*when the player rotates their ship, the angle changes, and it calls the ShipPic with the same number as the player's angle and places it on the screen.
All of this works perfectly.
Then, in Player::draw_screen(), I have it set up so that each player looks at all the other players and gets their distance. If they're within range, it takes the other player's picture rotated by the other player's angle and puts it on the current player's PlayScreen. This is where it freezes.
I've checked for NULL pictures, I've checked to be sure the angle is between 0 and 359, nothing makes any difference. I know it's reading the other player's information since I can output all of the player's X & Y coordinates, angles, the width/height of their pictures, etc. on each other's screens. So they're definitely talking.
To test the code, I've changed it from
Apply_Surface(ShipX, ShipY, p[i].ShipPic[(int)p[i].angle], PlayScreen);
to
Apply_Surface(ShipX, ShipY, p[ID].ShipPic[(int)p[ID].angle], PlayScreen);
And it works perfectly, placing the player's OWN picture in for the other players. So the function works. It's just when I try to take another player's picture and place it on the current player's screen that it freezes.
I've tried quite a few different ideas, such as creating a temp drawing surface to blit the other player's picture onto, but again, it freezes as soon as I try using the other player's pictures.
View 5 Replies
View Related
Mar 27, 2012
I've been having trouble working on this physics engine of mine. Right now I'm having trouble adding gravity.
The game is a spaceship--which you control--flying around in space with asteroids and eventually the ability to shoot bullets. There should be a wrap on the edges of the screen and gravity for each object depending on their mass.
I'm creating a forceX and forceY for each force put onto each object, and then computing that force into a velX and velY which will determine the direction of each object and at which speed.
Where my problem arises:
Code:
//add gravity pulls to forces
for(int i = 0; i <= pushCount; i++) //add gravity pulls for each object {
for(int u = 0 ; u <= pushCount; u++) //each object should add a force for every other object {
if(i != u) {
switch(id[i])
[Code] ....
View 7 Replies
View Related
Nov 7, 2013
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;
class Dice {
[Code] ....
Every time the roll_dice function get call twice for the same object, i receive the same dice values.
View 4 Replies
View Related
Feb 19, 2013
How do i read a specific part of a file? I am trying to create a game that reads the prices of an object inputted by the user from a file. This is the code i have so far
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream infile;
infile.open ("Objects.txt", ifstream::in);
[Code] ....
The file contains this :
Example objects
( ) store items
(item) (purchase value)/(sell value)
Grocery Store Items
Fish 5 7
Vegetables 10 15
Drinks 20 30
Weapon Store Items
Pistol 300 375
Rifle 400 500
Ammunition 20 30
View 4 Replies
View Related
Feb 14, 2013
How would one cycle through a turn order in a turn-based game? I was thinking an array of every creature (including the player) and have a pointer to the array++ after the turn, but I couldn't put all the objects into an array.
View 2 Replies
View Related
Jan 1, 2013
Let's say I want to calculate something like PI using some algorithm, but with the number of digits I want to.
View 1 Replies
View Related
Dec 21, 2014
I'm working on my first video game. So far I have a few classes in the game starting with the Game class which includes a list of GameObjects (another class). There are several classes that inherit from GameObjects used to implement things like bullets, explosions, various enemy types, etc.
The game essentially iterates through the list of GameObjects to update/render them. I would like to provide access to the Game's list of GameObjects inside another class (like the Bullet class) so I can put new objects on the list. For example, when a bullet hits, I want to add an explosion to the Game's GameObject list it can be updated/rendered.
How this should be setup? I was considering adding a pointer to the Game or GameObject list to the GameObject class (and methods to access it), but I was wondering if there is a better way to set this up?
View 4 Replies
View Related
Sep 13, 2014
I would like to make a program for calculating the total price of a game station, and a game. I made a program like this for just the price of a game in class, but I want to make one that does the game system as well.
View 7 Replies
View Related
Nov 6, 2012
Consider the following code which closes an overlapped I/O serial handle during application shutdown.
Code:
Win32Com.CancelIo(hPort);
Win32Com.CloseHandle(hPort);
It works fine under .NET 2.0 but after switching to .NET 4.0 it crashes on the CloseHandle. Removing CancelIO doesn't work.
What the correct way is to close an overlapped I/O handle? And why is there the difference between NET2.0 and 4.0?
View 5 Replies
View Related
Jul 30, 2013
How to handle cmd line argument in c.
View 5 Replies
View Related
May 29, 2013
I've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)
Thread should be the only way to do this:
btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};
the local variable thrd is destructed right after it starts, which may cause a crash, so use new operator:
btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};
The thrd never get deleted
How would you solve problem like this?
View 2 Replies
View Related
Apr 14, 2012
If a class A contains an array of objects of another class B.. And in the constructor of A, while constructing objects of B, any one object throws an exception, then how can we guarantee to release all the memory acquired.. so that there is no memory leak..
class B{};
class A{
public:
B Array[100];
...
};
In the above code, if in constructor of A, suppose 99 objects of B are constructed successfully, but 100th object throws exception, then how can we guarantee to release all the memory acquired by the other 99 objects?
View 1 Replies
View Related
Jan 18, 2013
I'm in the beginning stages of designing my first video game using SDL, and have run into a bit of an impasse. I attempt to draw the map using a simple tile function and nothing prints to the screen. I am going to attach the function itself.
bool Tile::draw_tiles ( void ) {
GV gv;
//Open the map
std::ifstream map ( "test.map" );
for ( int y = 0; y < GV::MAP_HEIGHT / 64; y++ ) {
for ( int x = 0; x < GV::MAP_WIDTH / 64; x++ ) {
[Code] ....
Extra notes:
1. Compiles fine.
2. No run time errors (this function returns true).
3. Apply surface is a function that takes an x and y value for position on the
map, an image, a screen to print it to, and a rectangle to cut out the image out of.
4. I did some bug testing and added another function to print the image SDLS_tiles by itself (its just a simple sprite map) after the map.close() function, and it worked fine.
View 1 Replies
View Related
Sep 14, 2013
I have a game that I'm trying to create but I don't really know how to handle mouse motions yet. I have read the LazyFoo.net lesson 9. And I have too searched around on Google but haven't find any good article for what I want to do
But what I want is to check if the mouse is OVER the START button of the game and if the user has pressed the LEFT mouse button the game should START.
This works when I am hovering over the area there I want the mouse to do something:
Code:
else if (event.type == SDL_MOUSEMOTION)
{
if (event.motion.x == 0)
{
quit = true;
}
}
I do not know how to check this. The START and QUIT button is in the middle of the screen and I don't know how to position the mouse there either.
View 5 Replies
View Related
Oct 19, 2013
I am using SDL 2.0. But I don't know how to handle mouse input. Do you think you can explain of how to do that? Or give me a link that explains that really great?
I know how to handle a mouse button that presses down for a sec.
Code:
if (event.type == SDL_MOUSEBUTTONDOWN){
if (event.button.button == SDL_BUTTON_LEFT){
// Do something . . .
}
}
But not how to handle the mouse input of what the mouse is hovering over and that.
Like in a start menu of a game. I want a button for example to appear blue when I hover over it and when I click on it. It should start the game for example.
View 6 Replies
View Related
Mar 11, 2013
I do not know any of the functions to do this, and when I looked on the internet all the functions I saw were mainly non-standard.
View 12 Replies
View Related
Feb 9, 2015
I have an application that uses an array of threads to call a method along with thread.join(). I was just wondering what would be the best way to handle the thread in case if one of the thread fails? Should I put a try catch block on the method that is being called or should I put the try catch block on the array of threads, or is there any other proper way to handle failed threads?
View 3 Replies
View Related
Nov 7, 2013
And it is not running successfully... abnormal termination
View 3 Replies
View Related
May 6, 2013
I write a small application and using the following API to open the file:
hHandle = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Then I pass the hHandle to a DLL exported function, in that function, I will call
DWORD dwFlags = 0;
ASSERT(GetHandleInformation(hFile,&dwFlags) != 0)
to check if the handle is valid. However, the assert fails. And I use GetLastError to get the error code 6, which means invalid handle.
What is the problem with my codes?
View 4 Replies
View Related
May 5, 2013
Visual C++ 2010
I'm using SFML with Visual C++ and need to pass a handle to a function. I've tried to find this on the web with no luck.
The handle happens to be a sprite defined as: sf::Sprite Numbers(MyNumbers);
Now I want to pass "Numbers" to a function.
-
-
getFrame(Numbers);
-
-
???? getFrame(??????) {
Numbers.SetSubRect(sf::IntRect(0,0,63,63));
return ????
}
How do I do this?
View 1 Replies
View Related
Nov 2, 2014
Here is an example,
Code:
int main()
{
try{
int y = 0;
int x = 2/y;
}
catch(...)
{
cout<<"catch it"<<endl;
}
return 0;
}
If you try, you will find out that catch(...) couldn't catch exception divided by zero. How'd we catch such exceptions?
View 4 Replies
View Related
Oct 11, 2012
I have an MFC program created from the app wizard. It is an MDI program, reading/writing text files using Serialize. I can read the document and know that the entire document was read into my buffer without any errors. This was verified by compaing the number of bytes read with the file length.
How do I get the document to display in my main/child window?
How do I read the document from the window so I can save it back to the file?
View 14 Replies
View Related
May 3, 2013
i have to make an application called " paint " in c++ . It should facilitate user to draw different shapes using a mouse. Now the biggest hurdle i am facing is the usage of mouse .. what is the header file for usage of mouse ? i am using visual studio 2010 . the syntax is creating problems ..
View 1 Replies
View Related