Archived

This topic is now archived and is closed to further replies.

sdlprorammer

Rotating a triangle in SDL

Recommended Posts

I want to write a single program on rotating a triangle. The trinagle will also be filled with a color, or with many colors. How do i go about it? Am i going to keep drawing the triangle pixel by pixel? And the movement is also hard.. PLEASE help

Share this post


Link to post
Share on other sites
Why not use OpenGL?

SDL has support for OpenGL integration, so this would be a breeze. There''s little sense in reinventing the wheel, unless this is for the sake of learning how 3D works.

Share this post


Link to post
Share on other sites
Yeah it sounds as if you try to learn SDL by reading tutorials about opengl or directx.
SDL is meant for sprites. For example: why would you want to rotate triangles in a 2d tile based game?

You can rotate sdl surfaces using an additional library though.
http://www.ferzkopp.net/~aschiffler/Software/SDL_gfx-2.0/

But if I were you I''d approach learning differently.
You can''t start a new thread for each function of SDL here ...

Share this post


Link to post
Share on other sites
It's not 3D. It's only 2D. I want to rotate from the center ( only a point ).I know how to move a sprite in the screen, but rotating the triangle is a little harder.. So?

P.S RuneLancer could you show me some functions.. ( without pure SDL, with OpenGL too - just too have a look at them )?

>You can't start a new thread for each function of SDL here ...
I am sorry but i am a beginner in graphics, and, i know.... every beginning is hard so bear with me...

[edited by - sdlprorammer on May 27, 2004 9:38:17 AM]

Share this post


Link to post
Share on other sites
Perhaps it would be wise to have a look at the SDL documentation that came with it? I learned everything I know about SDL from them, really; it's not too hard.

As for OpenGL functions, there's hardly any point in showing you example functions if they're taken out of context: it's a little hard to understand what the use of toying with the projection matrix is without explaining what the projection matrix is and what it's for, and how it affects an OpenGL scene as a whole. What you're looking for is a tutorial, pretty much. May I suggest having a look at NeHe? They have pretty good stuff to get you started.

On the subject of you asking so many questions about SDL, have you tried experimenting with these functions? A first-hand look at what happens when you use something can often be very insightful and could solve a lot of the questions you have. You can't expect to have the answer to everything spoon-fed to you when it comes to programming. And it'll make Clueless a happy man.

[edited by - RuneLancer on May 27, 2004 10:03:33 AM]

Share this post


Link to post
Share on other sites

RuneLancer, if you read my posts, you''ll see that i do read the docs, and that i do expirement.

Anyway, on the topic, why would i go on OpenGL when what i want is 2D? Please let''s leave OpenGL alone right now. Can anybody please give me some notes on how to implement what i need?

Share this post


Link to post
Share on other sites
I didn't write it because it is annoying.
With so many questions about specific functions ...
I think it is a better understanding of the big picture that would really help.
Read in the gamedev articles.
Look at SDL example programs.
Look at OpenGl example programs.
Read threads about 2d and 3d programming.

Looking at the SDL documentation alone does not make sense at that stage IMO.

Why would anybody want to rotate a triangle (why triangle???) in SDL?

[edited by - Clueless on May 27, 2004 10:23:44 AM]

Share this post


Link to post
Share on other sites
Rotation of graphics is getting beyond the basic 2D idea. When you want to start doing arbitrary rotations and scalings then you really need to look into 3D if you want speed. The SDL_gfx library may help you though.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
ah.... Well if i am going to download a library for that stuff ( which as i understand shouldn''t be done with SDL ), and one just for writting text in the screen, then why don''t i just drop SDL and start OpenGL, witch with i can do everything SDL supports?

Share this post


Link to post
Share on other sites
Why do you seem to think SDL and OpenGL are competing libraries. They are not. It''s like saying should I drop bananas in favour of a spoon.
You can quite happily use both together, one, or something different entirely to accomplish similar goals.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
OpenGL does not support everything SDL does.

SDL handles low level graphics, sound, input, event polling, etc.

OpenGL does 3d graphics.

SDL does not do 3d graphics, and OpenGL does not do low level graphics (without the lower level graphics, you have no way to get a context into which OpenGL can draw things).

SDL and OpenGL are both low level libraries. It is expected that there would be higher level libraries built on top of them. For example SDL can draw graphics to the screen, and a library for easily handling text can be built on top of it using the lower level graphics capabilities. OpenGL can draw polygons to the screen, and a higher level library can use this polygon drawing to support the drawing of complex models.

Share this post


Link to post
Share on other sites
Eeerm.. i think i''ll stay with SDL and then move to the part of OpennGl that enables me to draw poligons and 3D graphics. Will that be difficult? And is it a good choice? I mean, i can still do pretty nice stuff with SDL right?

thanks

Share this post


Link to post
Share on other sites
Maybe it would help if you could say what your goals for your first projects are!?
But I can already tell you:
If you think using fonts in SDL shouldn't be as hard as it is ...
then you will give up pretty soon no matter what the projects are.

Depth sorting, resource management, collision detection ... !

Game programming IS pretty tough.

You can take a look at a thread about a game using SDL (not finished though).
It is badly coded ... so it is just to give a better idea of what things you'll be dealing with:

Breaktris Thread

[edited by - Clueless on May 27, 2004 2:13:58 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
SDL does not limit what you can do with OpenGL in any way. You use SDL to create the context into which OpenGL draws things.

Using SDL without OpenGL you can draw sprites onto the screen, and even access the display buffer directly to mess with the individual pixels. It gives you the lowest level acces it possibly can while still staying cross-platform.

You can do 3d graphics yourself without OpenGL, but the math can be very complicated (even with OpenGL it is still complicated), the code you write probably wouldn''t be as optimized as the code OpenGL is using to draw stuff, and you''d have an extraordinarily difficult time trying to use any sort of hardware acceleration. The advantage of OpenGL for 3d is that it provides access to optimized drivers and hardware acceleration to draw things very very quickly.

Share this post


Link to post
Share on other sites
many many thanks for all the help you give me

>If you think using fonts in SDL shouldn''t be as hard as it is... then you will give up pretty soon no matter what the projects are.
Yes, i used to believe, but now i don''t, because,and most important, i *know* the *reason*.


Ok, i think this should go here: I plan in making a Tetris game for my first game. There, i should use polygons. I am not going to rotate them ( rotating a triangle is completely irrelevant ), but i''ll have to make them rotate in 4 states. you get my point? So, how would i draw this poligon? Or am i gonna use 2 or 3 (rectangular) surfaces for a poligon?

thanks..

Share this post


Link to post
Share on other sites
You only need rotation in 90 degree steps? That''s easy! It gets harder when you want arbitrary rotation, i.e. rotation for any kind of degree. I wrote a function that does this in SDL. Never used it, though. I can e-mail it to you if you want.

Share this post


Link to post
Share on other sites
Here''s my function for rotating in 90 degree steps. It''s not very pretty, and it''s not commented, although you don''t have to be Einstein to understand it.

http://www.rafb.net/paste/results/8vKlMu84.html

''direction'' is either 1, 2 or 3 for a rotation of 90, 180 or 270 degrees.

Share this post


Link to post
Share on other sites
thanks for the replies
I can't just use one Surface, because it is s rectungle. Say for example that i have this shape:


____
____| |
| |
--------


What method should i use to draw it, and keep it in memory ( uoi get my point anyway )?

[edited by - sdlprorammer on May 27, 2004 4:48:13 PM]

Share this post


Link to post
Share on other sites
sdlprogrammer: This is the basic SDL+OpenGL template programme that comes with Dev-C++ (a development environment similar to Visual C++). It draws a spinning triangle.


#include <SDL/SDL.h>
#include <windows.h>
#include <gl/gl.h>

int main(int Arg_N, char ** Arg_V){
SDL_Event event;
float theta = 0.0f;

SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

glViewport(0, 0, 600, 300);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);

for(int done = 0; !done{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(theta, 0.0f, 0.0f, 1.0f);

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();

theta += .5f;

SDL_GL_SwapBuffers();
SDL_PollEvent(&event);

if(event.key.keysym.sym == SDLK_ESCAPE)done = 1;
}

SDL_Quit();
return(0);
}


It uses SDL to open a window and handle key presses (press Esc to exit) but uses OpenGL to draw the triangle. OpenGL can do 2D graphics - visit www.opengl.org and find the OpenGL Red Book available for download. It can also (obviously) do 3D.

On the other hand, you might be better off looking for a library that adds to SDL''s functionality like SDL_image or SDL_gfx: http://www.libsdl.org/libraries.php?order=name&category=12&completed=100&os=any&match_name=SDL_&perpage=50

Share this post


Link to post
Share on other sites
You could store it as a 3x3 bool array which tells your block-drawing function how the block looks like. Then it''s only a matter of drawing small squares where the bools are true, skipping over the false ones.

Share this post


Link to post
Share on other sites
4x4 array.

You always blit the same tile image 4 times per block.

From that tutorial:


// 0 1 2 3 4 5 6
// X These
// X XX X XX XX XX XX are
// X XX XXX XX XX X X block
// X X X types


Think ahead ...
if you have one big image ... how does the collision detection work?

[edited by - Clueless on May 27, 2004 4:59:09 PM]

Share this post


Link to post
Share on other sites