Rotating a triangle in SDL

Started by
28 comments, last by sdlprorammer 19 years, 10 months ago
Why polygons? ... use sdl surfaces only.

Look at the math in that tutorial.
Tetris Tutorial

The blocks and the map will just be arrays of tiles.
You rearrange the tiles instead of rotating images.

[edited by - Clueless on May 27, 2004 4:58:03 PM]
Writing errors since 10/25/2003 2:25:56 AM
Advertisement
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]
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
[teamonkey] [blog] [tinyminions]
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.
2 + 2 = 5 for extremely large values of 2
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]
Writing errors since 10/25/2003 2:25:56 AM
well thanks for the help ( and for the very cool OpenGl code that makes me wanna start learning OpenGL )

Anyway, as i can understand from what u are saying, for the shapes, i won''t have to use any surfaces to store them. Just an array that will tell me what pixels i should draw in the screen to make that polygon. Is that good?
nobody???
Nehe
I read two of your threads (the other one was that OpenGL vs SDL). I think you are still confused what they are and what they do and what 2D and 3D really are.

2D:
This doesn''t only mean you only have width and height. SDL or DirectDraw is called 2D API is because they work on pixels. They copy pixels. That''s all what they do. Surfaces (SDL surfaces or DirectDraw surfaces) are used to hold image data in a form of pixels. When you draw something, what the API does is only copying the pixels. That''s it. pixels pixels and pixels. That''s why you only have width and height, or x and y because that''s all you need to copy pixels, the location of the pixels. That''s why you can''t rotate a triangle in SDL because you can''t rotate surfaces! Even if you could, you would get some visual artifacts. However, you can still rotate it by 90 degrees. It''s not actually rotating it, but more like it changes the way the pixels are copied. For example, instead of copying it from left to right, it''s copying it from right to left. Instead of top-bottom, bottom-top. The result is an inverted image. Open MS-Paint, and play around with their Flip and Rotate, you get the idea.


3D:
This does not work on pixels, but rather arbitrary units which is then projected to the screen. That''s why you don''t see surfaces in OpenGL because it doesn''t need them. All you do in OpenGL to draw a triangle is to specify three points. That''s it. Since it''s done in arbitrary units, rotation is possible. Just apply a rotation matrix to the points. and blam you are done.


Now about SDL. It''s 2D. It doesn''t do 3D. It is perfect if you intend to do that pixel-by-pixel method. You use SDL_Surface to hold your images, copy pixels, display stuff. That''s it.

But now how do you use 3D in SDL? You use OpenGL with SDL. You basically tell SDL "Hey SDL, I am going to use OpenGL to display my stuff. So I won''t be needing your surfaces, but I just need your window for OpenGL to use it." Once you have done it, you are done with SDL. Leave SDL alone, play with OpenGL. So SDL now is working like a bridge between you and OpenGL.
oh.. MANY MANY MANY THANKS for the kind and to-the-point answer!!!
Thank you everybody SSSSSSSOOOOOOO much!

This topic is closed to new replies.

Advertisement