I'm using an linux container(Chrome OS) and wanted to create an app with Allegro5.
I compile it using
g++ -Wall allegroTest.cpp `pkg-config --cflags --libs allegro-5`
My code is the most basic of allegro codes:
#include<iostream>
#include <allegro5/allegro.h>
int main() {
std::cout << "Loading Allegro5...\n";
if(!al_init())
{
std::cout << "Could not initialize Allegro5.\n";
return -1;
}
std::cout << "Loaded Allegro5. Declaring 'display' of type 'ALLEGRO_DISPLAY*'...\n";
al_set_new_display_flags(ALLEGRO_FULLSCREEN);//Edit 2
ALLEGRO_DISPLAY* display = al_create_display(800, 600);
if(!display)
{
std::cout << "Could not initialize ALLEGRO_DISPLAY.\n";
return -1;
}
std::cout << "Declared 'display' of type 'ALLEGRO_DISPLAY*'\n";
al_clear_to_color(al_map_rgb(0, 0, 0));
al_flip_display();
al_rest(5.0);
al_destroy_display(display);
return 0;
}
My thanks in advance.
Edit: I have done some debugging and it looks like al_create_display is causing the segmentation fault
Edit 2: I added the line al_set_new_display_flags(ALLEGRO_FULLSCREEN) and now it returns null
Related
This question already has answers here:
How do I use SDL2 in my programs correctly?
(3 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 months ago.
I've got the following piece of code attempting to use SDL_image;
#include <iostream>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_image.h>
int WIDTH = 800;
int HEIGHT = 800;
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) > 0) {
std::cout << "SDL INIT FAILED" << SDL_GetError() << std::endl;
}
SDL_Window* window = SDL_CreateWindow("Chaturanga", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (NULL == window)
{
std::cout << "Could not create window: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Texture* boardTexture = IMG_LoadTexture(renderer, "res/images/board.png");
if (boardTexture == NULL) {
std::cout << "Failed to load texture. Error: " << SDL_GetError() << std::endl;
}
SDL_Event windowEvent;
while (true)
{
if (SDL_PollEvent(&windowEvent))
{
if (SDL_QUIT == windowEvent.type)
{break;}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, boardTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
When I compile with:
g++ src/*.cpp -o main.exe -I lib/SDL2_lib/include -L lib/sdl2_lib/libr -L lib/sdl2_lib -lsdl2 -lsdl2_image
I get the following error;
My file structure is as follows, all functionality not relating to sdl image works fine.
Yes, the sdl_image.h file is in include;
Inside the libr folder:
I've searched extensively but there seems not to be a satisfying answer to this question online. So I'm posting again this problem in case someone has found a solution.
I have written a C++ code that is supposed to call some Matlab code. I have compiled all my Matlab files using the following command:
mcc -N -W cpplib:libRTR2 -T link:lib RTR.m -v
I have included the header, DLL and LIB files created by above command in the 'Header Files'for my Visual Studio project. I have also includde mclmcrrt.lib, mclmcrrt.h and mclcppclass.h in the same.
Here is my C++ code:
#define BZZ_COMPILER 3
#include <stdint.h>
#include "./hpp/BzzMath.hpp"
#include "libRTR2.h"
#include <iostream>
#include "mclmcrrt.h"
#include "mclcppclass.h"
double RTRtest(BzzVector &x)
{
x(1);
mwArray out(1,1);
try {
// create input
double a[] = { x[1] };
mwArray in1(1, 1, mxDOUBLE_CLASS, mxREAL);
in1.SetData(a, 1);
// call function
RTR(1, out, in1);
// show result
std::cout << "objFun" << std::endl;
std::cout << out << std::endl;
double F[1];
out.GetData(F, 1);
for (int i = 0; i<1; i++) {
std::cout << F[i] << " " << std::endl;
}
}
catch (const mwException& e) {
std::cerr << e.what() << std::endl;
return -2;
}
catch (...) {
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
// cleanup
return out;
}
int optim()
{
if (!mclInitializeApplication(NULL, 0)) {
std::cerr << "could not initialize the application" << std::endl;
mclGetLastErrorMessage();
return -1;
}
if (!libRTR2Initialize()) {
std::cerr << "Could not initialize the library" << std::endl;
return -1;
}
bzzFilePrint("BzzRobustMinimization.txt");
BzzVector Xmin(1, 2000.);
BzzVector Xmax(1, 5000.);
BzzVector X0(1, 2000.);
double F0 = RTRtest(X0);
BzzMinimizationRobust m(X0, F0, RTRtest, Xmin, Xmax);
m();
m.BzzPrint("Results");
libRTR2Terminate();
mclTerminateApplication();
}
int main()
{
mclmcrInitialize();
return mclRunMain((mclMainFcnType)optim, 0, NULL);
}
And when I try to debug it I obtain this error:
Access Violation Error
I know it is related probably to trying to pass a null pointer to one of my functions, but being no expert in C++ I'm struggling to find where the error lies. Any help is greatly appreciated!
I've got a path to my file defined this way:
const char* GROUND_TEXTURE_FILE = "objects/textures/grass.jpg";
And here is the function, which I use to load image:
bool loadTexImage2D(const string &fileName, GLenum target) {
...
// this will load image data to the currently bound image
// at first, we must convert fileName, for ascii, this method is fine?
wstring file(fileName.begin(), fileName.end());
if(ilLoadImage(file.c_str()) == IL_FALSE) { //here the program falls
What's wrong in my code? Why the program falls when ilLoadImage is called? I think, that file.c_str() should work fine as a wchar_t * type or not? Thanks for answer :)
As the author's said, you can do pretty anything without initializing the lib :D
#include <iostream>
#include <IL/il.h>
int main ()
{
std::string filename = "objects/textures/grass.jpg";
ilInit();
if (!ilLoadImage(filename.c_str())) {
std::cout << ilGetError() << std::endl;
return 1;
}
std::cout << ilGetInteger(IL_IMAGE_WIDTH) << std::endl;
std::cout << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl;
return 0;
}
build:
g++ -Wall -pedantic --std=c++11 -g -o app main.cpp -lIL
I am trying to use a cursor to seek a specific frame in stream.
main.cpp code follows:
#include <iostream>
#include <opencv2/opencv.hpp>
int main()
{
cv::VideoCapture cap;
if (!cap.open("8%d.jpg"))
return 1;
double index = cap.get(CV_CAP_PROP_POS_FRAMES);
index += 10.0;
std::cout << "Seek to index: " << std::endl;
if ( !(index < cap.get(CV_CAP_PROP_FRAME_COUNT)) )
return 1;
bool retval = cap.set(CV_CAP_PROP_POS_FRAMES, index);
assert(retval);
std::cout << cap.get(CV_CAP_PROP_POS_FRAMES) << std::endl;
cap.release();
return 0;
}
Command line on Ubuntu 14.4.3 LTS with OpenCV 2.4.11:
g++ main.cpp `pkg-config --cflags --libs opencv`
Command to check returning value:
echo "$#"
Output:
Seek to index: 10
-9.223377e+18
cv::VideoCapture::set function on an image sequence seems to break the stream, can you tell me why ?
Note: It works for a video as input but not an image sequence!
PS: I prefer asking before looking inside OpenCV-FFmpeg source code (it could take a while).
I'm trying to make this simple GraphicsMagick example as a node binding/addon. This code works as expected in OSX 10.6.7 with GraphicsMagick 1.3.15
#include <Magick++.h>
#include <iostream>
using namespace std;
int main(int argc,char **argv)
{
Magick::InitializeMagick(0);
Magick::Image image;
try {
image.read( "snow.jpg" );
image.scale("320");
image.write( "snow-scaled.jpg" );
}
catch( Magick::Exception &error_ ) {
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
cout << "Image scaled!" << endl;
return 0;
}
Compiling:
g++ scale.cpp `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
Running:
./a.out
Image scaled!
But making this code a node binding (0.6.14) just freezes (see full gist):
void AsyncWork(uv_work_t* req) {
std::cout << "AsyncWork..." << std::endl;
Baton* baton = static_cast<Baton*>(req->data);
baton->result = 12345; // Just a test
Magick::Image image; // <--- Freezes here!
image.read("snow.jpg");
std::cout << "Scaling..." << std::endl;
image.scale("200");
std::cout << "Done!" << std::endl;
image.write("snow-scaled.jpg");
// and baton->error to true.
}
Output when calling it from javascript:
AsyncWork...
Any ideas what's wrong?
On a side note, this actually works when compiled/run under Ubuntu!
Have you tried to initialise with Magick::InitializeMagick(0); in AsyncWork? Asynch functions run on pool threads.
You could always just git the finished GM addon here.