Jump to content
  • Advertisement

DanielWilkins

Member
  • Content Count

    36
  • Joined

  • Last visited

Community Reputation

108 Neutral

About DanielWilkins

  • Rank
    Member
  1. Thank you for the reply. Someone else suggested SDL_gfx which supports blitting semi transparent surfaces to other semitransparent surfaces (RGBA to RGBA). It works perfectly. To recap, the reason I do this is so I build the sprite completely and then ship it off to the renderer without sending them in pieces (8 or 9 render calls per explosion). Cheers!
  2. Bump. And I think I overly confused people. Here is what I am trying to do: I am trying to create a composite surface by blitting to that surface. I need the surface to be initially transparent and I need to blit the image I linked above which has transparency onto it. If need be, I can also color the background of the explosion image I linked pink and use an alpha key if that works with SDL. Any advice?
  3. Hi. I am writing a Bomberman clone and trying to blit parts of an explosion to a surface and then render that surface but have ran into problems. Here is my function: SDL_Surface* SpriteManager::buildExplosion(int id, SDL_Surface* image, int size) { // Create the surface that will hold the explosion image SDL_Surface* explosion = SDL_CreateRGBSurface(SDL_SRCALPHA, size * 32 , size * 32, 32, 0, 0, 0, 0 ); // Our source and destination surfaces SDL_Rect srcrect; SDL_Rect dstrect; int parentX = sprites[id].x; int parentY = sprites[id].y; int middle = size / 2; // Create the first image srcrect.x = sprites[id].imgBlockX * 32; // default for now srcrect.y = sprites[id].imgBlockY * 32; // default for now srcrect.w = 32; srcrect.h = 32; // Get the location it should be applied to dstrect.x = middle * 32; dstrect.y = middle * 32; dstrect.w = 32; dstrect.h = 32; // Apply the texture SDL_BlitSurface(image, &srcrect, explosion, &dstrect); return explosion; } As of now, it only blits the 32x32 tile at 0,0 in the image to the middle of the destination tile. I am 99% sure all the code here is correct as my debug output is consistent with where everything should be. What it ends up doing is producing an image that is the correct size BUT only has a 32x32 black tile in the upper left hand corner. I am wondering if there is a problem with the fact I am blitting from an image with an alpha mask. Here is the image: So to recap: I am blitting the first tile at 0, 0 with size 32x32 to the middle of the surface I have created. What ends up happening is that it only renders a single 32x32 black tile in the upper left hand corner of the created surface (at least I know the surface is rendering in the correct place) In the above picture, the upper left player dropped a bomb and it exploded. It had a power of 2 meaning that the box was 5 tiles in width and height. Let me know if you have any questions. Thank you.
  4. Hi. I am writing a Bomberman clone and would like some advice on the way most 2d engines communication between classes (still preserving object oriented class design). For instance, I wrote an entity manager which contains all of the main classes for the game. It's basically an encapsulation of the game to take it out of the main.cpp file. It handles updating all of the classes (players, bombs, items). Now I have gotten to an interesting point. I had the handleInput() function in my main.cpp file but have since moved it into my class manager. I guess my question is if there is an agreed upon way to have two classes communicate with eachother. For example, if I were to handle input in the player class for each player individually and the player hits space bar, what would be the best way to alert the bomb class to create a new bomb at the location provided by the player. Would some sort of event queue be the way to go. Here is a simple visualization of the classes: --EntityManager -Player -Bombs I want to keep the EntityManager as abstract as possible for reuse in other games so while updatePlayers() and updateBombs() (functions that invoke the update routine by each class) is fine by me, I want to shy away from things like addBomb() and more really specific stuff which should be in each class. Does anyone have any advice on this matter? Or maybe a good design document to look at? Thanks a bunch!
  5. DanielWilkins

    2D Engine Main Loop - Need advice

    Thank you both again. Last question as I have the whole thing implemented and working: Does SDL have an accurate enough timer or should I grab a more precise one? Any recommendations? Thanks!
  6. DanielWilkins

    2D Engine Main Loop - Need advice

    Hi all. Sorry I ducked out for a bit. The first implementation was a bastardization of the main loop on the article: http://gafferongames...-your-timestep/ To be honest, the physics and calculus nature of his implementation confused me so although I implemented it, I know it was incorrect. I have since redone the loop using dewitters last game loop (is this also fixed time step), and have gotten good results... Here's the thing. I am not here to make a quick game, I want this to be a solid engine I can produce a game on, extend and reuse. I know physics will eventually be part of the game and although I don't anticipate writing my own physics engine, I would love to know the actual logic behind it. Because I have been less than clear and confused, and you have all been incredibly helpful in providing articles and source, let me break down my understanding of it and I will ask questions. Again, my apologies. I will be basing my questions, understand off of: http://gafferongames...-your-timestep/ Please tell me what I have missed. -------------------------- Fixed delta time So the inherent problem here is the rate at which you update your game. You shouldn't just update the game as fast as possible as that will make programs/games run faster on faster computers and slower on slower computers. Not ideal. Variable delta time The logical solution (what I tried as well) is to measure the time it takes for each frame and record the time change (the delta). Then pass this into the physics simulation. This will provide consistent speed but differing physics results on different framerates. Also for some reason, they are keeping track of the total time represented by t, for some reason. Why is this? It is used in the integrate function which I understand to be an integral from Calculus. Semi-fixed timestep The solution to this is to ensure the delta time is small and the results will be (nearly) identical every time. If I understand correctly, this loop takes the last frame rate (i think) and then updates the physics continuously, decrementing the frametime until it is zero and finally renders the frame. FINAL SOLUTION The final solution is to have a fixed delta time (time between updates) so that your physics are run at regular intervals BUT at the same time, render independently. Again, I am confused by some of his functions like integrate. I assume this is his physics update, just the same as I would update a player's position. To do this, he caps the delta time which will affect physics at a specific time to avoid the spiral of death. The next part is where I am confused: while ( accumulator >= dt ) { previousState = currentState; integrate( currentState, t, dt ); t += dt; accumulator -= dt; } When he integrates, what exactly is he updating? How would that apply to moving a character across the screen rather than a calculus function? Then, when the accumulator is smaller than delta time, he calculates the alpha value which is interpolation? Then, he interpolated the position of the ball (and in my case, a character) by the value alpha (this makes sense) My main question about this last loop is about the code I linked above is what exactly is it doing. What does integrate do? Is it just a general physics function? What would I do instead for a simple bomberman clone during that time. ---------------- Thank you again. It's quite funny. I have pretty much everything else coded and yet, my main loop keeps changing. I really appreciate the time you guys have spent helping me. Sorry that I have learned this a bit slow. I will make sure to pay it forward and help others. Thanks!
  7. DanielWilkins

    2D Engine Main Loop - Need advice

    Fixed everything with floats. Movement still feels a little bit off. Not sure why... Anyone willing to test and let me know what you feel. Feels a lot smoother now compared to what it used to be but a weird issue seems to involve the character moving faster in the open part of the level. It check collision less there when aligned with the grid but I thought the whole purpose of this kind of loop was to remove temporal antialiasing (which is has) and make it steady rendering throughout. How would I modify my main loop to work at a fixed frame rate?
  8. DanielWilkins

    2D Engine Main Loop - Need advice

    I have made some progress but am still confused a bit: First of all, I keep track of locations in integers. I do this because I didn't assume you could be between pixels but now I realize when I am using doubles that effect the x and y values, I might have to make the switch. Edit: I definitely have to make the switch. It seems as if it was dropping the precision on the speed. 0.9 became 0 and 1 was way too fast. Here is my revised main loop: int main(int argc, char *argv[]) { // Initialize everything if(!init()) { errorLog.writeError("Failed to initialize engine components!"); return 1; } // Time variables double t = 0.0; const double dt = 0.01; double currentTime = SDL_GetTicks(); double accumulator = 0.0; // Run until the user has requested a quit while ( !quit ) { double newTime = SDL_GetTicks(); double frameTime = newTime - currentTime; if ( frameTime > 0.25 ) frameTime = 0.25; currentTime = newTime; accumulator += frameTime; // Loop until we are ready to render, updating as we go while ( accumulator >= dt ) { update( dt ); t += dt; accumulator -= dt; } // Get the time we use for interpolation const double alpha = accumulator / dt; // Interpolate the values player.interpolate(alpha); // Render the scene renderScene(); } return 0; } When I interpolate the values, I do so like this: void Player::interpolate(double alpha) { renderXPos = curXPos * alpha + lastXPos * (1.0 - alpha); renderYPos = curYPos * alpha + lastYPos * (1.0 - alpha); } My question is how delta time and time play a role in updating the physics. I have my speed as an integer at 3. It moves about how fast I want it. In the main player update function: void Player::update(double dt) { float pSpeed = speed * dt; ... collision handling, etc.. position updating... } Am I using delta time correctly? Should I be applying it to speed or did I miss the point. Thanks to everyone who has helped me understand this so far. Edit: Apologies for the formatting. Not sure why it undoes the formatting I have in my IDE.
  9. DanielWilkins

    2D Engine Main Loop - Need advice

    Beernuts, so the physics engine has the timestep built in and controls the aspect of updating the players? I am writing a simple Bomberman clone. Would using a robust physics engine be overkill? Even if so, I will use this method if I have something a bit more complicated. Thank you!
  10. DanielWilkins

    2D Engine Main Loop - Need advice

    Brilliant! Thank you. And yes BeerNuts, I will have a look at the source thank you.
  11. DanielWilkins

    2D Engine Main Loop - Need advice

    Thank you. I guess what I am confused about is the whole interpolation and the function addFrameTime. What exactly does it do. I understand that the logic updates until it is larger than the logic step and then when rendering the position values are interpolated. Are these values actually modified? Or are they just interpolated for the rendering (i.e. does the rendering actually modify the values or does it just modify them temporarily for the rendering and then they are permanently changed when the next update() function is run). What exactly does addFrameTime do? And do you pass this value to your rendering call? Is it possible that it can interpolate a value past collision and have something interpolated into a wall for instance? Thank you.
  12. Hi. I am developing a simple Bomberman clone using SDL and C++. Collision detection, movement, animation have all been implemented. The problem is that the character often seems to skip across the screen. I have researched this and found this to be temporal anti aliasing due to the frame rendering between updates. I have found two main solutions and wanted input. This engine code should be as smooth as possible and I want to reuse it so I will put in the effort now to understand it. I have found an article on fixed timestep. I really wish I could implement it but I am very confused about how it applies to a non physics application or maybe just to something simpler like moving a character across the screen. This is the article I am referring to: http://gafferongames...-your-timestep/ Is this method a little overboard for a non physics intense game? Can anyone elaborate on what it does and how I would apply it to simple position updates on an x/y plane. There is also another article I found which covers quite a few different main game loops: http://www.koonsolo....tters-gameloop/ I tried to implement the last option but found that often I got some unexpected warping where it would interpolate that I would be somewhere and then change its mind. It's possible it was a bug on my part (e.g. not factoring it into collision correctly). Does anyone have a solid 2d main game loop they can break down and explain. I mean, I understand how it works, the components relating to handling input, updating logic and rendering, I am just confused sometimes about how the time is applied to those updates. Thank you for your time. Here is my main loop as it stands: int main(int argc, char *argv[]) { const int TICKS_PER_SECOND = 60; const int SKIP_TICKS = 1000 / TICKS_PER_SECOND; const int MAX_FRAMESKIP = 10; double next_game_tick = timer.getTime(); int loops; // Initialize everything if(!init()) { errorLog.writeError("Failed to initialize engine components!"); return 1; } // While the user hasn't quit while(quit == false) { loops = 0; while( timer.getTime() > next_game_tick && loops < MAX_FRAMESKIP) { update(); next_game_tick += SKIP_TICKS; loops++; } } render.cleanup(); return 0; } Chanz
  13. DanielWilkins

    Collision detection for bomberman type game

    What directions? When moving up and left is when I can cut through the whole map. All you need to do is check 1 block. If holding up, just check the one block above, if holding up and left, check the upper left block, left block and upper block. Worst case is you are in a corner and hitting 3 blocks max. Also, when holding up and left, these checks should be separate for instance if I hit up and left, and there is collision with the block above, I should still move left while holding both of those. I see you have the halfway check and he will kind of curve/maneuver which I dont really like, but if u just did each collision check separate like (if left && no block left), move.x--; then separately (if up && no block up), move.y++; [/quote] Can you elaborate on what would would do instead of the "curve/maneuver". I thought it worked pretty good for allowing players to move through despite the rigid size. What do you not like about it. I can make it much smaller. Right now it is 15 pixels. What if it was half of that instead? Also, it seems you can only move through when going up and left. Thank you for noticing that. Doesn't make me throw out all the code. Any more opinions?
  14. DanielWilkins

    Collision detection for bomberman type game

    Sorry about that. The walkspeed is very slow. Here it is but a bit quicker so you can really test.
  15. I am trying to get back into SDL and 2d games by writing a simple Bomberman clone. I have gotten map loading, texture loading and a bunch of the logic worked out. Now I am trying to go about and figure out how to get collision detection working and facilitate easy movement throughout the map. Currently, my collision detection code is about 500 lines. It works with a few hiccups but there must be an easier way to do this. Each block is 32x32 and my player (although his head peaks over) is also 32x32. So for that, I implemented really rigid collision detection. It would simply see if it could move in a specific direction, get the distance to the block if there was one and then move until it hit it. Fair enough. Then I added something I call "nudging" where if the player was at a specific part of the block and tried to move, say, left, and was within a specific threshold, it would move him up or down and then left. Made it a lot better. But still I am getting errors. It seems diagonally, the player can still go through the blocks. I could add more checks against that but I figure I am doing this wrong, or just a way that isn't optimal. There is another error where the collision vector (my data is stored in the vector) will crash the program when the player queries a part that is out of range (when the player travels through the walls. I only end up checking four directions every frame but still, I think there has to be a better way (especially considering 500 lines of code just for collision detection!). What kind of collision detection do you recommend for a game like this? I definitely don't need to be checking all of the squares every frame. Thank you! I attached the program so you can see what I mean.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!