I have done the following using C++:
1) I used SDL_CreateWindow()
to create an SDL window
m_window = SDL_CreateWindow("My First Side-scroller
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
2) I used SDL_CreateRenderer()
to create a renderer for the SDL window
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
3) I used SDL_CreateTexture()
to create a texture
m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);
4) I created a buffer of type Uint32
to hold texture information which is passed to the SDL_Texture
m_buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];
5) I created a custom pixel drawing function which is use to draw pixels at a low level and pass it to the buffer.
void Screen::setPixel(int x, int y, Uint8 red, Uint8 green, Uint8 blue) {
// Checking for pixels off screen
// This is sufficient because the rand function can generate a negative number
if (x < 0 || x >= SCREEN_WIDTH || y < 0 || y >= SCREEN_HEIGHT) {
return;
}
Uint32 colour = 0;
colour += red;
colour <<= 8;
colour += green;
colour <<= 8;
colour += blue;
colour <<= 8;
colour += 0xFF;
m_buffer[(y * SCREEN_WIDTH) + x] = colour;
}
6) I used 5) along with other functions to help draw pixels on random locations in the window and make them move vertically and loop from the top of the window to the bottom of the window (like moving stars).
Stars::Stars()
{
m_x_star=((2.0*rand())/RAND_MAX)-1;
m_y_star=((2.0*rand())/RAND_MAX)-1;
}
void Stars::moveStar(){
// starSpeed is the desired change in co-ordinate position for each pixel plotted
double const starSpeed = 0.001;
if(m_y_star<-0.998 && m_y_star>-1) {
m_y_star=0.995;
}
m_y_star-=starSpeed;
}
1) to 6) works with no-error
So, onto my issue: Blitting an image using SDL_BlitSurface
seems to require that the window is created using SDL_Surface() for its third flag. I want to load a .png image of a spaceship I drew. Here is the code I use to load the image.
void Screen::update_spaceShip() {
SDL_Surface *Image;
Image= IMG_Load("spaceship_stock2.png");
SDL_BlitSurface(Image,NULL,m_window,NULL);
}
Error message:
cannot convert 'SDL_Window*' to 'SDL_Surface*' for argument '3' to 'int SDL_UpperBlit(SDL_Surface*, const SDL_Rect*, SDL_Surface*, SDL_Rect*)'
What am I doing wrong?