SDL2: libpng error: IDAT: chunk data is too large - c++

I'm following this tutorial on SDL2 for GameDev and I have run into this error while attempting to load PNG.
Here is my code:
void Game::Init(const char * title, int x_pos, int y_pos, int width, int height, bool fullscreen)
{
int flags = 0;
if (fullscreen) {
flags = SDL_WINDOW_FULLSCREEN;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
std::cout << "Subsystems Initialized" << std::endl;
window = SDL_CreateWindow(title, x_pos, y_pos, width, height, flags);
if (window) {
std::cout << "Window Created..." << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
std::cout << "Renderer Created..." << std::endl;
}
is_running = 1;
}
SDL_Surface * buf_surface = IMG_Load("assets/potato.png");
std::cout << SDL_GetError() << std::endl;
potato_texture = SDL_CreateTextureFromSurface(renderer, buf_surface);
SDL_FreeSurface(buf_surface);
}
This is the error:
libpng error: IDAT: chunk data is too large
Error reading the PNG file.
Any suggestions?
PS this is my first time posting sorry if I did anything wrong.

Related

SDL2 Wont draw Rectangles Correctly

I'm creating a window and drawing a box but for some reason instead of drawing a box, the screen is just changed to that color. I have attached a photo of how the window looks and I will attach the source code.
#include <iostream>
#include <SDL.h>
#undef main
using namespace std;
int SCREEN_WIDTH = 650;
int SCREEN_HEIGHT = 650;
int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *window = SDL_CreateWindow("Cells", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr) {
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderClear(renderer);
// renderTextures
SDL_Rect fillRect = { 122, 122, 122, 122};
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &fillRect);
SDL_RenderPresent(renderer);
}
return 0;
}
Doesn't Draw Correctly
SDL_RenderClear uses current draw colour, which you modified, so your clear and rectangle colour is the same. Set different clear colour (the one you want at background where nothing else is drawn) with e.g.
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// now draw your rectangles with different col

SDL MOUSEMOTION yrel is not giving a proper value

I created this easy function and it seems there is something wrong with it that I really don't see what it is...
Evnt.motion.yrel output crazy number.
void EnthropyGenerator::OpenWindow()
{
SDL_Window *EnthropyGeneratorWindow;
SDL_Renderer* Renderer;
SDL_Init(SDL_INIT_VIDEO);
EnthropyGeneratorWindow =
SDL_CreateWindow("Enthropy Generator",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WindowSizeX,
WindowSizeY,
SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS |
SDL_WINDOW_INPUT_GRABBED);
if (EnthropyGeneratorWindow == NULL)
{
ServerEngine::FatalError("Could not create window: " +
(std::string)SDL_GetError());
}
Renderer = SDL_CreateRenderer(EnthropyGeneratorWindow, -1, 0);
SDL_SetRenderDrawColor(Renderer, 10, 255, 0, 255);
SDL_RenderClear(Renderer);
SDL_RenderPresent(Renderer);
bool NeedMoreEntropy = true;
SDL_Event Evnt;
while (NeedMoreEntropy)
{
while (SDL_PollEvent(&Evnt))
{
if (Evnt.type == SDL_MOUSEMOTION)
{
std::cout << Evnt.motion.xrel << " and "
<< Evnt.motion.yrel << std::endl;
std::cout << m_EnthropyNeed << std::endl;
UpdateMousePosition(Evnt.motion.xrel, Evnt.motion.yrel);
AddEnthropy(m_MouseX, m_MouseY);
if (m_EnthropyNeed == 256)
{
NeedMoreEntropy = false;
}
}
}
}
SDL_DestroyWindow(EnthropyGeneratorWindow);
SDL_Quit();
}
void EnthropyGenerator::UpdateMousePosition(int deltaX, int deltaY)
{
m_MouseX += deltaX;
m_MouseY += deltaY;
}
void EnthropyGenerator::AddEnthropy(int deltaX, int deltaY)
{
m_EnthropyNeed++;
}
The output in the console is: 0 and 1985359926 and so on.
The output
It seems like something is not initialized or looks like a bad pointer. How can I fix this problem?

std::invoke no matching overloaded function found error given in VS 2015

I'm very new to C++ and SDL and I am trying to create a thread that constantly updates the screen but the I keep getting the following errors:
'std::invoke no matching overloaded function found'
and
'Failed to specialize function template 'unknown-type std::invoke(Callable &&,_Types&&...)''
main.cpp
int main(int argc, char **argv) {
using namespace std::placeholders;
bool gameover = false;
int test;
std::string filepath = getResourcePath("Lesson1");
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { // Intializes SDL functionality
std::cout << "Could not start SDL" << std::endl;
std::cin >> test;
return 1;
}
else {
std::cout << "SDL started successfully!" << std::endl;
}
viewWindow window; // Class representing the window in which the program is run.
SDL_Renderer *render = window.render(); // Pointer to the renderer used to draw images to the window.
if (render == nullptr) {
std::cout << "There was an error creating the renderer" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
SDL_Surface *emptySurface = window.blankSurface(); // Temp surface to draw background to
if (emptySurface == nullptr) {
std::cout << "Unable to create a blank surface " << std::endl << SDL_GetError() << std::endl;;
std::cin >> test;
return 1;
}
surfaces background;
background.filename = "grass.bmp";
SDL_Surface *backgroundSurface = background.loadSurface(filepath);
if (backgroundSurface == nullptr) {
std::cout << "Unable to create background surface" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
SDL_Rect backgroundRect;
SDL_Texture *backTexture = background.blitBack(render, backgroundRect, backgroundSurface, emptySurface);
player player;
SDL_Rect playerRect;
playerRect.x = 320;
playerRect.y = 240;
playerRect.h = 16;
playerRect.w = 16;
SDL_Texture *playerTexture = player.createPlayerTexture(render, filepath);
if (playerTexture == nullptr) {
std::cout << "Could not load player texture" << std::endl << SDL_GetError() << std::endl;
std::cin >> test;
return 1;
}
while (!gameover) {
std::thread t((&viewWindow::refreshWindow, render, playerRect, backTexture, playerTexture));
playerRect.x = player.moveX(playerRect);
playerRect.y = player.moveY(playerRect);
t.join();
}
return 0;
}
viewWindow.h
#pragma once
#ifndef VIEWINDOW_H
#define VIEWWINDOW_H
#include "SDL.h"
class viewWindow // Class representing the window.
{
private:
char winName[45] = "Game Test";
int winWidth = 640;
int winHeight = 480;
int xPos = 960;
int yPos = 540;
public:
SDL_Window *view(); // Intializing funtions for creating the window and renderer.
SDL_Renderer *render();
SDL_Surface *blankSurface();
void refreshWindow(SDL_Renderer *renderer, SDL_Rect &playerRect, SDL_Texture *backtex, SDL_Texture *playertex);
};
#endif
viewWindow.cpp
#include "viewWindow.h"
#include <string>
#include "SDL.h"
SDL_Window *viewWindow::view()
{
SDL_Window *createdwindow = SDL_CreateWindow(winName, xPos, yPos, winWidth, winHeight, SDL_WINDOW_SHOWN);
return createdwindow;
}
SDL_Renderer *viewWindow::render() {
SDL_Renderer *render = SDL_CreateRenderer(view(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
return render;
}
SDL_Surface *viewWindow::blankSurface() {
SDL_Surface *blacksurface = SDL_CreateRGBSurface(0, winWidth, winHeight, 32, 0, 0, 0, 0);
return blacksurface;
}
void viewWindow::refreshWindow(SDL_Renderer *renderer, SDL_Rect &playerRect, SDL_Texture *backtex, SDL_Texture *playertex) {
while (true) {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, backtex, NULL, NULL);
SDL_RenderCopy(renderer, playertex, NULL, &playerRect);
SDL_RenderPresent(renderer);
}
}
Method refreshWindow is not static. std::invoke requires the object instance of viewWindow class to call this method. You should pass it as a second parameter into the thread constructor:
std::thread t(&viewWindow::refreshWindow, window, render, std::ref(playerRect), backTexture, playerTexture);
Instead of function pointer you could use lambda function:
std::thread t([&](viewWindow* view){ view->refreshWindow(render, playerRect, backTexture, playerTexture); }, &window);

How do I draw a transparent or semi-transparent rectangle on top of an opaque one in SDL2?

Here's my code to draw SDL_Rect objects rect and rect2:
#include <iostream>
#include <SDL2/SDL.h>
int main(){
SDL_Window *window= SDL_CreateWindow("Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if(window == 0)
std::cout << SDL_GetError() << std::endl;
SDL_Renderer *renderer= SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(renderer == NULL)
std::cout << SDL_GetError() << std::endl;
SDL_Rect rect;
rect.x= 100;
rect.y= 100;
rect.w= 100;
rect.h= 100;
SDL_Rect rect2;
rect2.x= 150;
rect2.y= 150;
rect2.w= 100;
rect2.h= 100;
while(true){
Uint8 r,g,b,a;
r= 32;
g= 32;
b= 32;
a= 255;
if(SDL_SetRenderDrawColor(renderer, r, g, b, a) == -1)
std::cout << SDL_GetError() << std::endl;
if(SDL_RenderClear(renderer) == -1)
std::cout << SDL_GetError() << std::endl;
r= 255;
g= 255;
b= 255;
a= 255;
if(SDL_SetRenderDrawColor(renderer, r, g, b, a) == -1)
std::cout << SDL_GetError() << std::endl;
if(SDL_RenderFillRect(renderer, &rect) == -1)
std::cout << SDL_GetError() << std::endl;
r= 100;
g= 100;
b= 100;
a= 0;
if(SDL_SetRenderDrawColor(renderer, r, g, b, a) == -1)
std::cout << SDL_GetError() << std::endl;
if(SDL_RenderFillRect(renderer, &rect2) == -1)
std::cout << SDL_GetError() << std::endl;
SDL_RenderPresent(renderer);
}
return 0;
}
While I can draw rect to SDL_Renderer object renderer just fine, if I add rect2 it is always opaque, blocking view of rect. Even though I set rect2's alpha to 0, it still shows up opaque, blocking rect from view.
How do I fix this so rect2 is more transparent?
All you need to do is call
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
after creating your renderer to enable alpha blending with RenderDraw functions. (SDL documentation)

SDL image only shows occasionally

The code below is meant to show a picture of a rider in the top left corner of the SDL window. With this code the image occasionally does appear as intended, but most of the time when I run the program I just get a black window. Any idea why?
#include "Game.h"
#include <iostream>
bool Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullScreen)
{
int flags = 0;
if (fullScreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
//attempt to initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING)==0) {
std::cout << "SDL init success \n";
//init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (m_pWindow !=0) // window init successful
{
std::cout << "Window creation successful\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer !=0) // renderer init success
{
std::cout << "renderer creation successful\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}
else
{
std::cout << "renderer init failed!\n";
return false; // render init failed
}
}
else
{
std::cout << "Window init failed!\n";
return false; //window init failed
}
}
else
{
std::cout << "SDL init failed!\n";
return false; // SDL init failed
}
std::cout << "Init success\n";
m_bRunning = true; // everything init-d successfully, main game loop running
SDL_Surface* pTempSurface = SDL_LoadBMP("rider.bmp");
if(!pTempSurface)
{
std::cout << "Error, tempsurface is null";
}
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurface);
SDL_FreeSurface(pTempSurface);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceRectangle.w, &m_sourceRectangle.h);
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.x = m_sourceRectangle.x = 0;
m_destinationRectangle.h = m_sourceRectangle.h;
m_destinationRectangle.w = m_sourceRectangle.w;
return true;
}
void Game::render()
{
SDL_RenderClear(m_pRenderer); // clear the renderer to the draw color
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceRectangle, &m_destinationRectangle);
SDL_RenderPresent(m_pRenderer);
}
void Game::handleEvents()
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
m_bRunning = false;
break;
default:
break;
}
}
}
void Game::clean()
{
std::cout << "cleaning app\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();
}