I was wondering how exactly cameras are programmed in a 2D platform game. How is it programmed only to render whats in the view of the camera without rendering the whole map? Also, whats the proper way to do this?
Lazy foo has some good tutorials on this subject and further http://lazyfoo.net/SDL_tutorials/index.php
navigate to the scrolling tutorial, its in c++ with SDL but the logic should be universal.
There is no secret about that, you can simple check which tiles and which sprites are inside the rectangle that defines the screen and only draw those.
Another trick is to make the cameras always follows the player, but when it gets to the corner of the scenario you stop moving the camera, so you do not show the scenario borders.
Related
I’ve tried my best searching online and reading the documentation to find a good answer, but I have yet to find one.
I’m creating a multi-platform 2d side-scrolling retro game in Cocos2d-x v3. The engine is rather new to me, but I do have decent experience in C++, Xcode and game development.
The game which I’m trying to create is utilising super minimal pixel art and I’m at a loss as to decide which approach would be best. Currently I have two ideas:
Work with a fixed resolution (e.a. 240x120, I’m working with
really(!) small sprites) and use Design Resolution to scale the entire canvas
upwards to the screen size using kResolutionNoBorder.
Pros: I don’t have to scale my sprites and I can just use a short piece of code in the Init() function.
Cons: Not sure if you can use Design Resolution to scale pixel perfect?
Scale every sprite so that it appears to be running on a ±240x120
resolution.
Pros: ?
Cons: I have to scale every sprite which means more code and less clarity as to what is really happening on my canvas.
I read the multi-resolution-support page on the Cocos2d-x wiki, but it didn’t really give me advice on what would be the best approach for retro games.
I feel that I don’t have enough knowledge about Cocos2d-x to make the right decision yet, and a voice in my head says I’m forgetting something important.
There are more factors at play. How do want the game to look that are diffrent from 240*120.
I'll suggest scaling the screen for pixel art games. You can also control aliasing/anti aliasing param for all texture. Generally for pixel game, anti aliasing is turned off.
KResolutionNoBorder might crop your game edges to fit it in. There are other options,like kResolutuonFixedHeight, kResoltuionFitAll, depending to game requirement and HUD placement.
Well, it still can look pixel perfect in large screen, cocos2dx uses float for the size and position, so even if 1 pixel in your design view is represented as 5x5 pixels on the screen, when you move the position 0.2 point it will look like it moves 1 pixel. How precise it can be depends on the device screen resolution.
I would go for scaling the canvas
I am working on Box2D in C++. I have the world created. Is there some sort of shape, or inbuilt method, which can be used to write text into the world, and at it, maybe as a fixture? Maybe this is easy, but I haven't been able to figure it out!
Box2D provides only two basic shapes out of the box that you may probably already know: Polygons and Circles. If you need more advanced shapes, you should go for tools like box2d editor
In my case, I'm making a 2D game that camera always follow my character, but there will be scene limitations so I have to stop follow him when he is nearly beside scene borders. I also have to treat camera scale, so I have to decide whether to use CCFollow::actionWithTarget() or CCCamera.
In CCCamera.h it says:
Limitations:
- Some nodes, like CCParallaxNode, CCParticle uses world node coordinates, and they won't work properly if you move them (or any of their ancestors)
using the camera.
- It doesn't work on batched nodes like CCSprite objects when they are parented to a CCSpriteBatchNode object.
- It is recommended to use it ONLY if you are going to create 3D effects. For 2D effecs, use the action CCFollow or position/scale/rotate.
The last sentence is interesting, why ONLY use it in 3D effects? Seems CCCamera is not recommended by its producer. I know it's a shortcut to treat camera movements, but I just don't know why better not to use it in 2D games.
If you notice, the CCCamera.h file also says:
Useful to look at the object from different views.
The OpenGL gluLookAt() function is used to locate the
camera.
There's a good write up in the OpenGL Redbook on how the camera works. My understanding is, the camera is meant to setup the initial orientation, then you move the world by updating the model matrix (which is what I'm guessing CCFollow does for you). There's an answer here that may help if you still want to use the camera:
moving CCCamera
I plan on making a game (in SDL) where, if one character moves, the part of the image it was on turns alpha, thus allowing me to place a scrolling image underneath the original scene.
1) Is this possible?
2) If yes to #1, how can I go about implementing this (not to give me code, but to guide me in the right direction).
It sounds like you want to learn about image compositing.
A typical game these days will have a redraw function somewhere to redraw the entire screen. The entire scene is always redrawn each frame.
void redraw()
{
drawBackground();
drawCharacters();
drawHUD();
swapBuffers();
}
This is as simple as it gets: by using the right blending modes, each time you draw something it appears on top of what was drawn before. Older games are much more complicated because they don't redraw the entire screen at a time (or don't use a framebuffer), and newer games are much more complicated because they draw the world front-to-back and back-to-front in multiple passes for different types of objects.
SDL has software image compositing functions which you can use, or you can use OpenGL (which may use a combination of software and hardware). I personally use OpenGL because it is more powerful (lets you draw more complicated scenes), but the SDL compositing functions are easier to use. There are many excellent tutorials and many more mediocre or terrible tutorials online.
I'm not sure what you mean when you say "the part of the image it was on turns alpha". The alpha channel does not appear on screen, you cannot see it, it just affects how two images are composited.
I guess I'll illustrate with an example:
In this game you are able to draw 2D shapes using the mouse and what you draw is rendered to the screen in real-time. I want to know what the best ways are to render this type of drawing using hardware acceleration (OpenGL). I had two ideas:
Create a screen-size texture when drawing is started, update this when drawing, and blit this to the screen
Create a series of line segments to represent the drawing, and render these using either lines or thin polygons
Are there any other ideas? Which of these methods is likely to be best/most efficient/easiest? Any suggestions are welcome.
I love crayon physics (music gets me every time). Great game!
But back to the point... He has created brush sprites that follow your mouse position. He's created a few brushes that account for a little variation. Once the mouse goes down, I imagine he is adding these sprites to a data structure and sending that structure through his drawing and collision functions to loop through. Thus popping out the real-time effect. He is using Simple DirectMedia Layer library, which I give two thumbs up.
I'm pretty sure the second idea is the way to go.
First option if the player draws pure freehand (rather than lines), and what they draw doesn't need to be animated.
Second option if it is animated or is primarily lines. If you do choose this, it seems like you'd need to draw thin polygons rather than regular lines to get any kind of interesting look (as in the crayon example).