• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MylesEvans

I need help drawing a square using SDL/Opengl.

4 posts in this topic

[source lang="cpp"]/*This source code copyrighted by Lazy Foo' Productions (2004-2012)
and may not be redistributed without written permission.*/

//The headers
#include "SDL.h"
#include <SDL_opengl.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")


bool initGL();
bool init();
void handleKeys( unsigned char, int , int);
void update();
void render();
void clean_up();

//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The frame rate
const int FRAMES_PER_SECOND = 60;

//Event handler
SDL_Event event;

//Rendering flag
bool renderQuad = true;

//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;

//The ticks stored when the timer was paused
int pausedTicks;

//The timer status
bool paused;
bool started;

public:
//Initializes variables
Timer();

//The various clock actions
void start();
void stop();
void pause();
void unpause();

//Gets the timer's time
int get_ticks();

//Checks the status of the timer
bool is_started();
bool is_paused();
};

bool initGL()
{
//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );

//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}

return true;
}

bool init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}

//Create Window
if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
{
return false;
}

//Enable unicode
SDL_EnableUNICODE( SDL_TRUE );

//Initialize OpenGL
if( initGL() == false )
{
return false;
}

//Set caption
SDL_WM_SetCaption( "OpenGL Test", NULL );

return true;
}

void handleKeys( unsigned char key, int x, int y )
{
//Toggle quad
if( key == 'q' )
{
renderQuad = !renderQuad;
}
}

void update()
{

}

void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );

//Render quad
if( renderQuad == true )
{
glBegin( GL_QUADS );
glVertex2f( -0.5f, -0.5f );
glVertex2f( 0.5f, -0.5f );
glVertex2f( 0.5f, 0.5f );
glVertex2f( -0.5f, 0.5f );
glEnd();


}

//Update screen
SDL_GL_SwapBuffers();
}

void clean_up()
{
//Quit SDL
SDL_Quit();
}

Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}

void Timer::start()
{
//Start the timer
started = true;

//Unpause the timer
paused = false;

//Get the current clock time
startTicks = SDL_GetTicks();
}

void Timer::stop()
{
//Stop the timer
started = false;

//Unpause the timer
paused = false;
}

void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;

//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}

void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;

//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;

//Reset the paused ticks
pausedTicks = 0;
}
}

int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}

//If the timer isn't running
return 0;
}

bool Timer::is_started()
{
return started;
}

bool Timer::is_paused()
{
return paused;
}

int main( int argc, char *argv[] )
{
//Quit flag
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

//The frame rate regulator
Timer fps;

//Wait for user exit
while( quit == false )
{
//Start the frame timer
fps.start();

//While there are events to handle
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
else if( event.type == SDL_KEYDOWN )
{
//Handle keypress with current mouse position
int x = 0, y = 0;
SDL_GetMouseState( &x, &y );
handleKeys( event.key.keysym.unicode, x, y );
}
}

//Run frame update
update();

//Render frame
render();

//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}

//Clean up
clean_up();

return 0;
}
[/source]

Ok, ignore the timer code, my problem is (that I cant seem to find a solution to after days of searching) is the drawing of the square.

You see how its a 0.5x0.5 square? Well its 0.5 of the screen. Run this code on your computer and see. IDK why its like this. If the screen size is 640x480 then shouldnt the square be very small on the screen? I dont want it to be 0.5 of the screens width/height but just 0.5 units long.


I've used freeglut and could draw squares fine without any problems. Now that I'm using SDL/Opengl together I have this problem.

When I change the screen size it changes the entire window size. When I draw a square thats 1.0x1.0 for example its not drawn as 1/the screens size, but as 1 = a whole. So the square thats 1x1 is automatically the entire screen regardless of its size and a square 0.5x0.5 is automatically half the screens size. I need to fix this.
0

Share this post


Link to post
Share on other sites
The modelview and projection matrices are both identity matrices, which means the visible coordinate range is from -1 to 1 along all axes. If you draw a quad that extends from -0.5 to 0.5, then, on a coordinate system that is from -1 to 1, you'll get a quad that is half the size of the screen.

If you want another coordinate system, then you have to set up another coordinate system as well. Look up glOrtho to begin with. Edited by Brother Bob
0

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1349359279' post='4986766']
The modelview and projection matrices are both identity matrices, which means the visible coordinate range is from -1 to 1 along all axes. If you draw a quad that extends from -0.5 to 0.5, then, on a coordinate system that is from -1 to 1, you'll get a quad that is half the size of the screen.

If you want another coordinate system, then you have to set up another coordinate system as well. Look up glOrtho to begin with.
[/quote]K, thank you. I'm new to all this. Still learning.
0

Share this post


Link to post
Share on other sites
That's what you do with glOrtho.

It takes, ignoring the two last ones for the moment, two pairs of parameters. First one is the horizontal range of the visible coordinate system (from left to right), and the second one is the vertical range of the visible coordinate system (from bottom to top).

If you want the visible coordinate system to range from left=0 to right=640 along the horizontal direction, and from bottom=0 to top=480 along the vertical direction, you call:
[code]
glOrtho(0, 640, 0, 480, -1, 1);
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0