SDL_LoadBMP() is successful, but the window becomes entirely black - c++

I apologize if this question has already been asked but I've been researching for about a week now and cannot find the answer any where.
The problem that I am having is that while SDL_LoadBMP() is loading the image successfully, The window does not render an image at all, and instead renders an entirely black screen. However I do know something is being loaded (not just because there is no error being returned by SDL_LoadBMP() but also) because when I run the program with the SDL_LoadBMP() call commented out the window stays entirely white.
if it helps I have been writing along with the Lazyfoo tutorial located here. Code below...
From Main.cpp
int main(int argc, char* args[])
{
//the surface that we will be applying an image on
SDL_Surface* ImageSurface = NULL;
//try to initalize SDL
try
{
initSDL();
}
//if an error is caught
catch (string Error)
{
//print out the error
cout << "SDL error occurred! SDL Error: " << Error << endl;
//return an error
return -1;
}
//try loading an image on to the ImageSurface
try
{
loadMedia(ImageSurface, "ImageTest.bmp");
}
//if an error is caught
catch(string Error)
{
//print the error out
cout << "SDL error occurred! SDL Error: " << Error << endl;
//return an error
SDL_Delay(6000);
return -1;
}
//Apply Image surface to the main surface
SDL_BlitSurface(ImageSurface, NULL, Surface, NULL);
//upadte the surface of the main window
SDL_UpdateWindowSurface(Window);
//wait for 2 seconds (2000 miliseconds)
SDL_Delay(10000);
//close SDL
close();
//return
return 0;
}
From SDLBackend.cpp (I will only be posting the code relevant to the image loading process)
void loadMedia(SDL_Surface* surface, string path)
{
cout << "Attempting to load an image!" << endl;
//load the image at path into our surface
surface = SDL_LoadBMP(path.c_str());
//if there was an error in the loading procdure
if(surface == NULL)
{
//make a string to store our error in
string Error = SDL_GetError();
//throw our error
throw Error;
}
cout << "Successfully loaded an image!" << endl;
cout << "Pushing surface into the Surface List" << endl;
//Put the surface in to our list
SurfaceList.push_back(surface);
return;
}
I am compiling using visual studio 2013, and the image ImageTest.bmp is located in the same directory as the vcxproj file.

The problem is in loadMedia(). The loaded surface is being assigned to a local variable. You'll need to use a reference to pointer,
void loadMedia(SDL_Surface*& surface, string path)
{
surface = SDL_LoadBMP(path.c_str());
}
Or a double pointer (maybe preferred, clarifies intent),
void loadMedia(SDL_Surface** surface, string path)
{
*surface = SDL_LoadBMP(path.c_str());
}
Alternately, you could return it or even extract it from SurfaceList.back().

Related

Why isn't SDL_QueryTexture assigning values to its int* arguments?

An adjustment in my code is causing a strange occurrence which is causing segfaults. After running it through gdb, I found that a call to SDL_QueryTexture wasn't assigning the SDL_Texture's width and height values to the int pointers passed to the method. I called SDL_GetError() and printed it, and it says: "Failed loading SHCORE.DLL: The specified module could not be found." While doing searching, I heard this may have to do something with older versions of Windows. I have Windows 7, but the code was working from before, so I doubt Windows is the problem here, but the code. The code I think is causing the problem (which includes the SDL_QueryTexture call) is below:
struct TextTexture {
private:
SDL_Texture* texture;
SDL_Rect destinationrect;
int* w;
int* h;
void loadText(string s) {
SDL_Color color = {255,255,255};
SDL_Surface* textsurface = TTF_RenderText_Solid(font, s.c_str(), color);
if(textsurface == NULL) {
cout << "Could not rendertext onto surface" << endl;
}
texture = SDL_CreateTextureFromSurface(r,textsurface);
if(texture == NULL) {
cout << "Could not make texture" << SDL_GetError() << endl;
}
SDL_QueryTexture(texture, NULL, NULL, w, h);
cout << SDL_GetError() << endl;
SDL_FreeSurface(textsurface);
textsurface = NULL;
}
void textRender(int x, int y) {
destinationrect = {x,y,*w,*h};
if (SDL_RenderCopy(r,texture,NULL,&destinationrect) < 0) {
cout << "Rendercopy error" << SDL_GetError() << endl;
}
}
};
The problem with your code is that you are simply passing the int variables by value and not by pointer.
SDL was written in C, and due to this it does not have pass by reference unlike C++. So, to allow SDL to input values into your variables you need to pass the pointers to those variables
You would change this line of code:
SDL_QueryTexture(texture, NULL, NULL, w, h);
To this:
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
The ampersands return the addresses of the variables w and h.
If you would like to learn more about pointers in C++ here's a link:
how does the ampersand(&) sign work in c++?

OpenCV imwrite is not working

I have a simple OpenCV application that takes a video stream from the webcam, and when the spacebar is pressed it captures the current image and freezes on that image. When I try to use the cv::imwrite() method to save the picture to disk, it does not work. The code successfully compiles but it does not save the image. It is returning a false value as well from the call. I am not sure if this is an issue of type of image or something else, but I seem to be stumped.
Here is the code for my current cpp class:
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
Mat picture;
char key;
class FacialRec {
};
int main() {
//Starting the video
VideoCapture videoCapture(0);
if (!videoCapture.isOpened()) {
cout << "Unable to open video file." << endl;
return 1;
}
namedWindow("Webcam", CV_WINDOW_AUTOSIZE);
while(true) {
Mat frame;
videoCapture.retrieve(frame);
bool success = videoCapture.read(frame);
if (!success) {
cout << "Could not read from video file" << endl;
return 1;
}
imshow("Webcam", frame);
key = waitKey(30);
if (key == 27) { //escape key pressed: stop program
cout << "ESC pressed. Program closing..." << endl;
break;
}else if (key == ' ') { //spacebar pressed: take a picture
picture = frame;
key = -1;
while (true) {
imshow("Webcam", picture);
key = waitKey(30);
if (key == 27 || key == 32) {
cout << "ESC or SPACE pressed. Returning to video..." << endl;
break;
}
if (key == 115) {
//trying to save to current directory
bool maybe = imwrite("/testimage.jpg", picture);
// maybe bool is always getting value of 0, or false
cout << "s was pressed. saving image " << maybe << endl;
}
}
}
}
return 0;
}
You are attempting to write testimage.jpg to the / directory. The executing program probably doesn't have sufficient permissions to write to that directory. Based on your comment, you probably want
//trying to save to current directory
bool maybe = imwrite("./testimage.jpg", picture);
Since . denotes the current working directory.
OpenCV sometimes has problems to write to a .jpg image. Try to change that to .png or .bmp to see if that makes a difference in your case.
If you have further issues with writing images, you can debug them in OpenCV by adding this few lines of code to display them and see if they are valid:
// Create a window for display.
namedWindow( "Display window", WINDOW_AUTOSIZE );
// Show our image inside it.
imshow( "Display window", picture );
// Wait for a keystroke in the window
waitKey(0);
A few suggestions.
try a different file format.
does your IDE defiantly know your target folder / home path directory?
Is your image definitely valid? does it show when you imshow()?

opencv stereo camera error

i working in a stereo camera project i have two cameras 5megapixels in every one i connected it with my laptop and run my code but when i run it i get this error libv4l2: error turning on stream: No space left on device
im linux os that's my c++ opencv code there are any ideas how to fix it i tried others codes i found it in network but still give me the same error
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture cap1(1);
cv::VideoCapture cap2(2);
if(!cap1.isOpened())
{
std::cout << "Cannot open the video cam [1]" << std::endl;
return -1;
}
if(!cap2.isOpened())
{
std::cout << "Cannot open the video cam [2]" << std::endl;
return -1;
}
cap1.set(CV_CAP_PROP_FPS, 15);
cap2.set(CV_CAP_PROP_FPS, 15);
// Values taken from output of Version 1 and used to setup the exact same parameters with the exact same values!
cap1.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap1.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cap2.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap2.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
cv::namedWindow("cam[1]",CV_WINDOW_AUTOSIZE);
cv::namedWindow("cam[2]",CV_WINDOW_AUTOSIZE);
while(1)
{
cv::Mat frame1, frame2;
bool bSuccess1 = cap1.read(frame1);
bool bSuccess2 = cap2.read(frame2);
if (!bSuccess1)
{
std::cout << "Cannot read a frame from video stream [1]" << std::endl;
break;
}
if (!bSuccess2)
{
std::cout << "Cannot read a frame from video stream [2]" << std::endl;
break;
}
cv::imshow("cam[1]", frame1);
cv::imshow("cam[2]", frame2);
if(cv::waitKey(30) == 27)
{
std::cout << "ESC key is pressed by user" << std::endl;
break;
}
}
return 0;
}

I got an error with sdl2 ttf and emscripten

i was porting my game to emscripten. Everything was fine until SDL_ttf.
Actually i am using sdl2 + sdl2 image + sdl mixer.
I will show an example:
SDL_Color color = {255,255,255};
std::cout << "1\n";
font = TTF_OpenFont("saucery/font/font1.otf", 8);
if (!font)
printf("Unable to load font: %s \n", TTF_GetError());
std::cout << "2\n";
SDL_Surface *surf = TTF_RenderText_Solid(font,"Oieee",color);
std::cout << "3\n";
if (surf){
std::cout << (int)surf << "\n";
texture = SDL_CreateTextureFromSurface(Game::instance->GetRenderer(),surf);
std::cout << "4\n";
Uint32 format;
int acess,w,h;
SDL_QueryTexture(texture, &format,&acess,&w,&h);
dimensions2.x = 0;
dimensions2.y = 0;
dimensions2.h = h;
dimensions2.w = w;
SDL_FreeSurface(surf);
}
In this code i just open an font (i have already changed the size and the type to .ttf). Everything seems to run fine until:
SDL_CreateTextureFromSurface
I put some std::cout to see where the code was "crashing". Everytime it calls the createTextureFrom on the console shows to me "45" and the running stops there.
Even with any std::cout i still get this error.
this is driving me crazy already ._.

Getting opencv error in c++

I'm try to get the error of opencv! say I have this program:
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
int key = 0;
while(key !=27){
cap >> frame;
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}
when I run this program I get in the console this message :
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown functi
on, file ..\..\..\opencv\modules\highgui\src\window.cpp, line 261
My question is how can I get this message and save it in a string for every error that I get!
and if it'S possible escaping the program crash!
thanks in advance!
It uses C++ exceptions. See here in the doc for more.
try
{
... // call OpenCV
}
catch( cv::Exception& e )
{
const char* err_msg = e.what();
std::cout << "exception caught: " << err_msg << std::endl;
}
A CV_Assert in the OpenCV code is a macro which calls the OpenCV function error. That function can be seen here. It will always print the error text on stderr unless you don't have the customErrorCallback set. You do that via cvRedirectError, see here.
You have to check whether OpenCV function calls in your code is successfully executed or not. Then you can understand the exact problem. Here is the modified code.
int main (){
cv::Mat frame;
cv::VideoCapture cap(1); // I don't have a second videoinput device!
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
int key = 0;
while(key !=27){
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video cam" << endl;
break;
}
cv::imshow("frame",frame);
key = cv::waitKey(10);
}
cap.release();
return 0;
}