Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Putpixel problem really need help guys

Started by Ultimate_Fusion Nov 27, 2007 at 7:05 AM 2 replies 2.8k views
Original Post
Ultimate_Fusion
Ultimate_Fusion
I've been trying to put pixels on the screen to use as particles, I managed to do it in another graphical engine but SDl doesnt like it.


#include "stdafx.h"

#include "ins.h" // SDL includes headers
#include <time.h>
#include <stdlib.h>

#include <math.h>
#include "helicopter.h"
#include "particle.h"
#include "Singleton.h"
//#include "SDL_Draw.h" // tried adding this didn't work

Singleton *sc2;

particle::particle()
	{
		for(int i=0;i<MAXPARTICLES;i++){
		  mrgAlive=false;
		}
	}

	void particle::Draw()
	{
	// Draw all the particles
	// if they are alive		
          for(int i=0;i<MAXPARTICLES;i++){
	      if(mrgAlive == true){
	         sc2 = Singleton::Instance();
		 // Lock surface if needed
                 if (SDL_MUSTLOCK(sc2->screen)) 
                 if (SDL_LockSurface(sc2->screen) < 0) 
                     return;

	       putpixel(10, 10, 0xffffff);
               putpixel(11, 10, 0xff0000);
               putpixel(10, 11, 0xff0000);
               putpixel(11, 11, 0xff0000);

               // Unlock if needed
               if (SDL_MUSTLOCK(sc2->screen)) 
               SDL_UnlockSurface(sc2->screen);

	    
	}

void particle::Move()
{
	for(int i=0;i<MAXPARTICLES;i++)
	{
           mrgPartX += mrgPartVX;
	   mrgPartY += mrgPartVY;
		  
	}
}


void particle::putpixel(int x, int y, int color)
{
  unsigned int *ptr = (unsigned int*)sc2->screen->pixels;
  int lineoffset = y * (sc2->screen->pitch / 4);
  ptr[lineoffset + x] = color;
}


i know its the putpixel(10, 10, 0xffffff); line or function because I commented then out and it compiled. I got the following errors: thanks for looking thanks ToohrVyk fixed one problem as least but as know may know every solution breeds new problems. i got: Access violation writing location 0x0212d58c. and it pointed to this line - ptr[lineoffset + x] = color; any ideas. I also added if (mrgPartX>640||mrgPartX<0) mrgAlive==false; if (mrgPartY>480||mrgPartY<0) mrgAlive==false; to stop drawing the particles when there go off the screen. [Edited by - Ultimate_Fusion on November 27, 2007 7:31:06 AM]
ToohrVyk
ToohrVyk
The error message is pretty clear. It says that it couldn't find the implementation of a function of signature "public: void __thiscall particle::putpixel(int,int,int)". Now, can you point to a definition, in your code, of such a function? (Hint: there isn't). Once you've found it, the error should be pretty obvious to correct.
samuraicrow
samuraicrow
Is that a current copy/paste of your existing code!?

Look for missing curly braces in your Draw method for starters.

Your range check should be something like this:
// put these in a header or at the beginning// of your code in case you want to switch resolutionsconst int HRESOLUTION=640;const int VRESOLUTION=480;// Now you can do this wherever (note the >= where the > used to be)if (mrgPartX>=HRESOLUTION||mrgPartX<0)  mrgAlive==false;if (mrgPartY>=VRESOLUTION||mrgPartY<0)  mrgAlive==false;


To make your code portable to 64-bit architectures, your putpixel method should use something like:
void particle::putpixel(int x, int y, unsigned int color){  unsigned int *ptr = static_cast<unsigned int *>(sc2->screen->pixels);  int lineoffset = y * (sc2->screen->pitch / sizeof(unsigned int) );  ptr[lineoffset + x] = color;}

or better yet:
void particle::putpixel(int x, int y, unsigned int color){  Uint32 *ptr = static_cast<Uint32 *>(sc2->screen->pixels);  // eliminates a divide and a scratch variable  *(ptr + (sc2->screen->pitch * y) + x*sizeof(Uint32) )=color;}


Better still, use the Uint32 typedef when using 32 bit unsigned integers. That will guarantee a 32-bit integer even if you are using a 64-bit or 16-bit operating system so you can port your code to an embedded controller and build it into a stand-alone joystick someday [wink].
Ultimate_Fusion
Ultimate_Fusion
thats samuraicrow im using windows 64 nit and my performance of particles has been boosted about 20%.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.