SDL_PollEvent in a loop? why???

Started by
18 comments, last by rip-off 11 years, 12 months ago
quote:Original post by sdlprorammer
If i am right, then i don''t need the inner loop cause the queue contains all the events...
so?


so? what is it good for to have all the events in the queue if you let them rot for half an eternity because you handle only one event per frame? everytime you get more than one event per frame your queue gets longer and longer, your event handling is getting more and more behind and at some point sdl will say "forget it, queue is full" and you start losing events.

pollevents is meant to GET events from the queue. that it also calls pumpevents to "fill" the queue is nothing but an annoying side effect so you dont have to do it yourself (and reason why tend to stay away from it and instead call pump events once and then use getevent).
f@dzhttp://festini.device-zero.de
Advertisement
Ok MANY MANY thanks for all the help! i get it now
The SDL_PumpEvents() function is there if you dont want to use SDL''s event loop. You can just poll the keyboard/joystick/mouse instead. The PumpEvents function updates the state of the keyboard/joystick/mouse etc.
PumpEvent ( from the doc ) places all the current events in the queue. Won''t the event go there by their own?

Yes, the events will go there "on their own" because PollEvents calls PumpEvents for you implicitly. Unless you are not using PollEvents, you should never need to worry about it.
ok thanks!
Can any of you guys show us a short example of how SDL_PumpEvents() would be used?
When you''re doing a tight loop waiting for the mouse state (SDL_GetMouseState or whatever) to change. It will not change unless SDL_PumpEvents() is called in that loop.
sorry, everybody i am trying to move around a map along with several other problem that affictl my program i have a post in this forum maybe you can check. anyway your discussion it is interesting for me because i am trying to use two sdl_mousebutton down and sdl_mousemotion. what i am trying to do it is using sdl button down as a flag and sdl_mousemotion to calculate the offset.
i have create a header file done like this [CODE]
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#define TILE_SIZE 40
#include"wrconv 0.01.h"
#include <vector>

using namespace std;

class Tile
{
struct tiledef
{
int tileID, typeID;
};
typedef vector<tiledef> LineType;
typedef vector<LineType> GridType;
GridType grid;
public:
enum { TILE_TYPE_NONE = 0, TILE_TYPE_VILLAGE, TILE_TYPE_GRASS};
SDL_Surface* Surf_Tile;
SDL_Surface* Surf_backgroundtwo;

Tile();
public:
void LoadFromFile(string filename);
void SaveToFile(string filename);
void tilerender(SDL_Surface* screen,int camerax, int cameray);
bool grabthemouse(SDL_Event* event,int ghrt);
void movethemouse(SDL_Event* event,SDL_Surface* screen);
};
[/CODE]

and a cpp like this

[CODE]
#include "tile.h"
#include"wrconv 0.01.h"
#include <math.h>
using namespace std;
Tile::Tile() {
Surf_Tile = NULL;
Surf_backgroundtwo = NULL;

}
void Tile::LoadFromFile(std::string filename)
{
ifstream infile(filename.c_str()); // open the file as an input stream
string line;
while (getline(infile, line)) // read the next line from the stream into a string
{
LineType row;
istringstream ss(line); // create an input stream from the string we read from the file
tiledef value;
char c;
while (ss >> value.tileID >> c >> value.typeID) // read the next set of coordinates from the file
{
row.push_back(value); // push the value pair onto a row
}
grid.push_back(row); // push the row onto the grid
}
}
void Tile::SaveToFile(string filename)
{
ofstream outfile(filename.c_str()); // create the output file as a stream
for (size_t row = 0; row < grid.size(); ++row)
{
for (size_t col = 0; col < grid[row].size(); ++col)
{
if (col != 0)
outfile << " ";
outfile << grid[row][col].tileID << ":" << grid[row][col].typeID;
}
outfile << endl;
}
}
void Tile::tilerender(SDL_Surface* screen,int camerax, int cameray)
{

Surf_Tile = IMG_Load( "C:/wrconv 0.01/bin/Debug/images/tile2.bmp" );
ofstream testfile("C:/wrconv 0.01/bin/Debug/maps/inf.txt");
int TilesetWidth = Surf_Tile->w /TILE_SIZE ;
int TilesetHeight = Surf_Tile->h /TILE_SIZE ;

for (size_t row = 0; row < grid.size(); ++row)
{
for (size_t col = 0; col < grid[row].size(); ++col)
{

int ID = grid[row][col].tileID;
int TilesetX = (ID % TilesetWidth) * TILE_SIZE;
int TilesetY = (ID / TilesetWidth) * TILE_SIZE;
int mapwidth = grid[row].size() * TILE_SIZE;
int mapheight = grid[col].size() * TILE_SIZE;
int centermapx = mapwidth / 2 ;
int centermapy = mapheight / 2;
int x = (col * TILE_SIZE + ( 320 - centermapx))- camerax;
int y = (row * TILE_SIZE + (240 - centermapy))- cameray;
testfile << camerax << " "<< cameray << endl;
wrconw tli;
tli.map(screen,Surf_Tile,x,y,TilesetX,TilesetY, TILE_SIZE, TILE_SIZE);

}
}
}
bool Tile::grabthemouse(SDL_Event* event){
if(event->type == SDL_MOUSEBUTTONDOWN){
if (event->button.button == SDL_BUTTON_LEFT){
int c = event->button.x;
int d = event->button.y;
if( ( c > 0 ) && ( c < 640 ) && ( d > 0) && ( d < 480 ) ){ return true; }
}
}
if(event->type == SDL_MOUSEBUTTONUP){
if (event->button.button == SDL_BUTTON_LEFT){
int c = event->button.x;
int d = event->button.y;
if( ( c > 0 ) && ( c < 640 ) && ( d > 0) && ( d < 480 ) ){ return false; }

}
}

}


void Tile::movethemouse(SDL_Event* event, SDL_Surface* screen){
if(event->type == SDL_MOUSEMOTION){
int camerax=0;
int cameray=0;
int x1 = event->motion.xrel;
int y1 = event->motion.yrel;
int x2 = event->motion.x;
int y2 = event->motion.y;

//-----------------------------
if (x1 == x2) {
if (y1 > y2){ camerax = 0; cameray= y2-y1;
}
if (y1 < y2){ camerax = 0; cameray= y1-y2;
}
}
//------------------------------
if (y1 == y2) {
if (x1 > x2){ cameray = 0; camerax= x2-x1;
}
if (x1 < x2){ cameray = 0; camerax= x1-x2;
}
}
//---------------------------------------1
if (( x1 > x2 ) and (y1 > y2)){
camerax = cameray = sqrt(pow ((x1 - x2),2) + pow ((y1 - y2),2));
}
//-----------------------------------------------------2
if (( x1 < x2 ) and (y1 > y2)){
camerax -= sqrt(pow ((x2 - x1),2) + pow ((y1 - y2),2));
cameray = sqrt(pow ((x2 - x1),2) + pow ((y1 - y2),2));
}
//-----------------------------------------------------3
if (( x1 > x2 ) and (y1 < y2)){
camerax = sqrt(pow ((x1 - x2),2) + pow ((y2 - y1),2));
cameray -= sqrt(pow ((x1 - x2),2) + pow ((y2 - y1),2));
}
//-----------------------------------------------------4
if (( x1 < x2 ) and (y1 < y2)){
camerax -= sqrt(pow ((x2 - x1),2) + pow ((y2 - y1),2));
cameray -= sqrt(pow ((x2 - x1),2) + pow ((y2 - y1),2));
}
//-----------------------------------------------------
tilerender(screen,camerax,cameray);
}

}

[/CODE]


how would you put the main? something like that doesn't work


while( t.grabthemouse(&event) == true ){ //while the left button it is pressed
t.movethemouse(&event,screen);//calculate the map offset and move the map around

SDL_Flip(screen);
}
This is a thread from 2004. Please do not post to old threads.

This topic is closed to new replies.

Advertisement