I'm new to SDL & I'm trying to render a simple image & this is my code:
#include <SDL.h>
#include<SDL_image.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(int argc, char* args[])
{
SDL_Window* window = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gHelloWorld = NULL;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("SDL Tutorial", 100, 50, 500, 500, SDL_WINDOW_SHOWN);
gScreenSurface = SDL_GetWindowSurface(window);
IMG_Init(IMG_INIT_PNG);
SDL_Surface* optimizedSurface = SDL_ConvertSurface(IMG_Load("glow.png"), gScreenSurface->format, 0);
SDL_BlitSurface(optimizedSurface, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
but when I run it, I'm getting this type of output
and here's how the image really looks like
I can't understand what I'm doing wrong
Related
I am trying to get an SDL_Rect to appear on screen with a texture from a bitmap. I run this program and the screen is simply white, with no image.
#include "SDL.h"
int main(int argc, char** args) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = NULL;
window = SDL_CreateWindow("window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface* surface = SDL_LoadBMP("car.bmp");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = 1280;
rect.h = 720;
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I got it working, I simply had to add SDL_RenderPresent().
The only thing I see is the RenderDrawColor.
Also the "circle.png" is in the right folder (where the main.cpp is).
#include <SDL.h>
#include <SDL_Image.h>
int main(int argc,char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);
IMG_Init(IMG_INIT_PNG);
SDL_Window* window = SDL_CreateWindow("_", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(render,0, 255, 255, 255);
SDL_Surface* img = IMG_Load("circle.png");
SDL_Texture* texture = SDL_CreateTextureFromSurface(render, img);
SDL_FreeSurface(img);
SDL_Event event;
while (1) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
return 0;
}
}
SDL_RenderClear(render);
SDL_RenderCopy(render, texture, NULL, NULL);
SDL_RenderPresent(render);
}
system("PAUSE");
return 0;
}
Had to include zlib1.dll and libpng16-16.dll to the same folder as the cpp.
libpng16-16.dll depends on zlib1.dll.
I have been having trouble displaying an image in an SDL window and I am not quite sure what I am doing wrong. The code compiles just fine and the image has been placed in the debug folder with my .exe so I am not sure why it is not displaying. Is there something minor I may have missed?
#include <iostream>
#include <stdio.h>
#include <SDL.h>
#undef main
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
int main(int argc, char* args[]) {
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_SHOWN);
SDL_Surface* image = SDL_LoadBMP("image.bmp");
SDL_Renderer* render = SDL_CreateRenderer(window, -1, 0);
SDL_Texture* texture1 = SDL_CreateTextureFromSurface(render, image);
SDL_RenderCopy(render, texture1, NULL, NULL);
SDL_RenderPresent(render);
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
SDL_DestroyTexture(texture1);
SDL_DestroyRenderer(render);
SDL_FreeSurface(image);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
If you step through the code in a debugger, and inspect the
return-values in each step, do you get something unexpected?
Such as a nullpointer. That is probably going to help you
narrow it down.
image has been placed in the debug folder with my .exe
If you run from the IDE, the current working folder is going
to be the project folder.
Here is some code in SMFL
RenderWindow window(VideoMode(320, 480), "The Game!");
Texture t1,t2,t3;
t1.loadFromFile("images/tiles.png");
t2.loadFromFile("images/background.png");
t3.loadFromFile("images/frame.png");
Sprite s(t1), background(t2), frame(t3);
Does SDL 2.0 has functions like this and how to convert them to SDL 2.0
yes, all is there:
https://programmersranch.blogspot.kr/2014/03/sdl2-animations-with-sprite-sheets.html
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Window * window = SDL_CreateWindow("SDL2 Sprite Sheets",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,
480, 0);
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * image = IMG_Load("spritesheet.png");
SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer,
image);
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_FreeSurface(image);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}
This is just a training excersize to get myself working with SDL, ignore the lack of functions, warnings and that everything is in main. I keep getting a segmentation fault, it is certainly what i've done with the section under the //Initialise TTF, create font pointer, set font and colour then render. section, as the code without that works fine.
#include <SDL2/SDL.h>
#include <SDL/SDL_ttf.h>
#include <stdio.h>
const int SCREEN_WIDTH = 1000;
const int SCREEN_HEIGHT = 1000;
int main(int argc, char* args[])
{
// Set up window, surface, image and font pointers
SDL_Window* gWindow = NULL;
SDL_Surface* gScreenSurface = NULL;
SDL_Surface* gImage1 = NULL;
SDL_Surface* gImage2 = NULL;
SDL_Surface* text = NULL;
// Get a window surface, load images
gWindow = SDL_CreateWindow( "Image in a window, and some text", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
gScreenSurface = SDL_GetWindowSurface(gWindow);
gImage1 = SDL_GetWindowSurface(gWindow);
gImage2 = SDL_GetWindowSurface(gWindow);
gImage1 = SDL_LoadBMP("./Image1.bmp");
gImage2 = SDL_LoadBMP("./Image2.bmp");
------------------------------------------------------
//Initialise TTF, create font pointer, set font and colour then render.
TTF_Init();
TTF_Font* font;
font = TTF_OpenFont("./LucidaBrightDemiBold.ttf", 24);
SDL_Color text_color = {255, 255, 255, 255};
text = TTF_RenderText_Solid(font, "Using TTF to write stuff", text_color);
-------------------------------------------------------
// Apply the image, update the surface with the image
SDL_BlitSurface(gImage1, NULL, gScreenSurface, NULL);
SDL_BlitSurface(gImage2, NULL, gScreenSurface, NULL);
SDL_BlitSurface(text, NULL, gScreenSurface, NULL);
SDL_UpdateWindowSurface( gWindow );
SDL_Delay(5000);
// Free surfaces
SDL_FreeSurface(gImage1);
SDL_FreeSurface(gImage2);
SDL_FreeSurface(text);
// Nullify surfaces
gImage1 = NULL;
gImage2 = NULL;
text = NULL;
//Close and nullify window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit images
TTF_Quit();
SDL_Quit();
return 0;
}
I think the problem lies between the -------'s.