I've been trying to create a simple C++ game in Visual Studio 2017, but I can't even get a simple black screen. The window comes up white and unresponsive, is anyone able to help? I've been learning from a free course on Udemy, it has been working up until now. My code is below.
#include <iostream>
#include <SDL.h>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
const int screenWidth = 800;
const int screenLength = 600;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL init faliure" << endl;
return 0;
}
SDL_Window *window = SDL_CreateWindow("Particle Fire", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenLength, SDL_WINDOW_SHOWN);
SDL_Delay(100000);
if (window == NULL) {
SDL_Quit();
return 2;
}
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, screenWidth, screenLength);
if (renderer == NULL) {
cout << "Could not produce renderer";
SDL_DestroyWindow(window);
SDL_Quit();
return 3;
}
if (texture == NULL) {
cout << "Could not produce texture";
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 3;
}
Uint32 *buffer = new Uint32[screenWidth*screenLength];
memset(buffer, 0xFF, screenWidth*screenLength*sizeof(Uint32));
for (int i=0; i < screenWidth*screenLength; i++) {
buffer[i = 0xFFFF0000];
}
SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
bool quit = false;
SDL_UpdateTexture(texture, NULL, buffer, screenWidth * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture , NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} }
}
delete buffer;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
I've tried changing everything, but it doesn't work.
You have a 100 second delay immediately after you created the window SDL_Delay(100000);.
Also, you need to change buffer[i = 0xFFFF0000] to buffer[i] = 0xFFFF0000. The first only sets i and leaves the buffer unchanged. The second makes the pixel yellow.
Related
I'm currently trying to set up a few C++ libraries for a future project. Namely, SDL2.
Here's my code:
#include <iostream>
#include <fstream>
#include <SDL.h>
int SCREEN_WIDTH = 457;
int SCREEN_HEIGHT = 497;
const char* imgpath = "Sprite.bmp";
std::string errmsg;
SDL_Window* window = NULL;
SDL_Surface* screensurface = NULL;
SDL_Surface* image = NULL;
SDL_Rect rect;
struct {
bool wdown;
bool adown;
bool sdown;
bool ddown;
bool edown;
bool escdown;
} kpresses;
void clearkpresses() {
kpresses.wdown = false;
kpresses.adown = false;
kpresses.sdown = false;
kpresses.ddown = false;
kpresses.edown = false;
kpresses.escdown = false;
}
void setrect() {
rect.x = 0;
rect.y = 0;
rect.w = 457;
rect.h = 497;
}
bool gameinit() {
std::ofstream errfile;
errfile.open("errlog.txt");
clearkpresses();
setrect();
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0 ) {
errmsg = "SDL could not initialize! SDL_Error:";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
screensurface = SDL_GetWindowSurface(window);
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if( window == NULL ){
errmsg = "Window could not be created! SDL_Error:";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
image = SDL_LoadBMP(imgpath);
if (image == NULL) {
errmsg = "Media unable to load! IMG Error: ";
std::cout << errmsg << SDL_GetError() << std::endl;
errfile << errmsg << SDL_GetError() << std::endl;
errfile.std::ofstream::close();
return false;
}
return true;
}
void gamehalt()
{
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
int main(int argc, char* args[]) {
if (!gameinit()) {
gamehalt();
return 0;
}
bool quit = false;
SDL_Event event;
while (!quit) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
}
switch(event.type) {
case SDLK_w:
kpresses.wdown = true;
case SDLK_a:
kpresses.adown = true;
case SDLK_s:
kpresses.sdown = true;
case SDLK_d:
kpresses.ddown = true;
case SDLK_e:
kpresses.edown = true;
case SDLK_ESCAPE:
kpresses.escdown = true;
}
}
//TODOUpdate
//TODORender
SDL_BlitSurface(image, NULL, screensurface, &rect);
SDL_UpdateWindowSurface(window);
//reset
clearkpresses();
}
gamehalt();
return 0;
}
Compiled, assembled, and linked with this windows cmd command:
g++ Main.cpp -static-libgcc -static-libstdc++ -I..\include\SDL2\include\SDL2 -I..\include\SDL2_image\include\SDL2 -L..\include\SDL2\lib -L..\include\SDL2_image\lib -w -lmingw32 -lSDL2main -lSDL2 -o ../Transcend.exe
It compiles and runs with no errors, but only displays a blank screen.
The problem is that you are trying to get the window surface, but the window isn't already created.
Please swap those two lines:
screensurface = SDL_GetWindowSurface(window);
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
This way:
window = SDL_CreateWindow("Transcend", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screensurface = SDL_GetWindowSurface(window);
Unfortunately I do not get a picture but only a white screen. I am currently learning c ++ and sdl.
error message:
SDL_image Error: Couldn't open test.jpg
I am using Visual Studio on a Windows 10 computer.
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;;
string ExePath() {
char buffer[MAX_PATH];
GetModuleFileName(NULL, buffer, MAX_PATH);
string::size_type pos = string(buffer).find_last_of("\\/");
return string(buffer).substr(0, pos);
}
int main(int argc, char* args[]) {
cout << "my directory is " << ExePath() << "\n";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
std::cout << "SDL load fail" << std::endl;
return -1;
}
SDL_Window* window = SDL_CreateWindow("Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cout << "Window load fail." << std::endl;
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
std::cout << "Renderer load fail." << std::endl;
return -1;
}
SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
if (background == NULL) {
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
SDL_Delay(5000);
return -1;
}
SDL_Rect pos;
pos.x = 20;
pos.y = 30;
pos.w = 460;
pos.h = 300;
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
atexit(SDL_Quit);
return 0;
}
I have already tried the absolute path. The picture is also in the correct folder. Smaller pictures, jpeg, png. I do not know.
SDL Image is a separate library. You should initialize it befor using it. Something like this:
////...
int imgFlags = IMG_INIT_JPG; // or IMG_INIT_PNG;
// not sure about the imgFlags parameter, read the docs.
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
}
/// the rest is the same...
SDL_Texture* background = IMG_LoadTexture(renderer, "test.jpg");
if (background == NULL) {
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
SDL_Delay(5000);
return -1;
}
/// ...
Some reference https://discourse.libsdl.org/t/help-with-initializing-sdl-image/23601
EDIT: And for the "one step closer": I think your delay is in the wrong place.
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, &pos);
SDL_RenderPresent(renderer);
SDL_Delay(5000); //////// For example try putting int here
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
atexit(SDL_Quit);
std::cout << "Bye!" << std::endl;
return 0;
As you can see below there is bmp image but its background color is white:
How can i change it to be for example black (without editing the bmp image itself)?
Here I have code to show bmp image on window:
#include <iostream>
#include "SDL.h"
using namespace std;
int main(int argc, char *args[])
{
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Surface *surface = nullptr;
SDL_Texture *texture = nullptr;
SDL_Event event;
SDL_Rect positionRect;
SDL_Rect dimensionRect;
positionRect.x = 0;
positionRect.y = 0;
positionRect.w = 100;
positionRect.h = 100;
dimensionRect.x = 0;
dimensionRect.y = 0;
dimensionRect.w = 300;
dimensionRect.h = 300;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 1;
}
if (SDL_CreateWindowAndRenderer(500, 500, SDL_WINDOW_OPENGL, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
return 1;
}
surface = SDL_LoadBMP("sample.bmp");
if (!surface) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface from image: %s", SDL_GetError());
return 1;
}
texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture from surface: %s", SDL_GetError());
return 1;
}
SDL_FreeSurface(surface);
surface = nullptr;
while (1) {
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
break;
}
//SDL_SetRenderDrawColor(renderer, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, &positionRect, &dimensionRect);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
You should add these 2 lines,
Uint32 colorkey = SDL_MapRGB(surface->format, 255, 255, 255);
SDL_SetColorKey(surface, SDL_TRUE, colorkey);
before this line in your code
texture = SDL_CreateTextureFromSurface(renderer, surface);
i am tryanig to do a SDL initialazion class but i get this error on a line thats not even in the file
C2440 : cannot convert from initialisers list to screen
what have i done wrong this is SDL types i have included SDL and it works fine so no error there the error seems to be in the constructor
#include "stdafx.h"
#include "Screen.h"
using namespace std;
Screen::Screen()
: m_window(NULL)
, m_renderer(NULL)
, m_texture(NULL)
, m_buffer(NULL)
{
}
bool Screen::Init()
{
if ((SDL_INIT_VIDEO) < 0) {
return 1;
}
m_window = SDL_CreateWindow("Particle Simulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);
if (m_window == NULL) {
SDL_Quit();
return 2;
}
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC);
m_texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, SCREEN_WIDTH, SCREEN_HEIGHT);
if (m_renderer == NULL) {
SDL_DestroyWindow(m_window);
SDL_Quit();
return 3;
}
if (m_texture == NULL) {
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
return 4;
}
m_buffer = new Uint32[SCREEN_WIDTH * SCREEN_HEIGHT];
memset(m_buffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * sizeof(Uint32));
m_buffer[30000] = 0xFFFFFFFF;
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) {
m_buffer[i] = 0xFFFFFFFF;
}
SDL_UpdateTexture(m_texture, NULL, m_buffer, SCREEN_WIDTH * sizeof(Uint32));
SDL_RenderClear(m_renderer);
SDL_RenderCopy(m_renderer, m_texture, NULL, NULL);
SDL_RenderPresent(m_renderer);
return true;
}
bool Screen::processEvents()
{
return false;
}
void Screen::close()
{
delete[] m_buffer;
SDL_DestroyRenderer(m_renderer);
SDL_DestroyTexture(m_texture);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
So here's my code:
#include "stdafx.h"
int main(int argc, char *argv[])
{
bool quit = false;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = NULL;
window = SDL_CreateWindow("RPG GAME", 100, 100, 600, 400, SDL_WINDOW_SHOWN );
if (window = NULL)
{
std::cout << "Window couldn't be created" << std::endl;
return 0;
}
SDL_Renderer* renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event* mainEvent = new SDL_Event();
SDL_Texture* grass_image = NULL;
grass_image = IMG_LoadTexture(renderer, "grass.bmp");
SDL_Rect grass_rect;
grass_rect.x = 10;
grass_rect.y = 50;
grass_rect.w = 250;
grass_rect.h = 250;
while (!quit && mainEvent->type != SDL_QUIT)
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(renderer);
//Cia darom
SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete mainEvent;
return 0;
}
Whenever I run the application, the window pops up but it's fully white, I've recently set up a dual monitor, and now having some issues while playing GIFs, but I think it doesn't matter. Thank you!
You need to distinguish between '=' and '=='. As in
if (window = NULL) // bad news cause you just nuked window
versus
if (window == NULL) // is window null?