Fatal error: exception Graphics.Graphic_failure("Cannot open display ") - ocaml

I was trying to run the code and it keeps showing the same error.
I start by compiling with ocamlc -o cardioide graphics.cma cardioide.ml and it appears to work, but then I do ./cardioide to execute it and the message Fatal error: exception Graphics.Graphic_failure("Cannot open display ") appears...
I've searched all across the internet and i can't find the solution, can someone please help me?
Thank you
open Graphics
let () = open_graph "300x20"
let () =
moveto 200 150;
for i = 0 to 200 do
let th = atan 1. *. float i /. 25. in
let r = 50. *. (1. -. sin th) in
lineto (150 + truncate (r *. cos th))
(150 + truncate (r *. sin th))
done;
ignore (read_key ())
Error message:
Fatal error: exception Graphics.Graphic_failure("Cannot open display ")

The string argument to the open_graph function is not the size or title, but actually implementation-dependent information that is passed to the underlying graphical subsystem (in X11 it is the screen number). In modern OCaml, optional arguments are passed using labels, but Graphics was written long before this feature was introduced to the language. Therefore, you have to pass an empty string there (if you don't want to pass any specific to implementation of the underlying graphical subsystem information), e.g.,
open_graph ""
will do the work for you in a system-independent way.
Besides, if you want to resize the window, then you can use the resize_window function. And to set the title, use set_window_title.
For the historic reference, the string parameter passed to the open_graph is having the following syntax (it is no longer documented, so there is no reason to believe that it will be respected):
Here are the graphics mode specifications supported by
Graphics.open_graph on the X11 implementation of this library: the
argument to Graphics.open_graph has the format "display-name
geometry", where display-name is the name of the X-windows display
to connect to, and geometry is a standard X-windows geometry
specification. The two components are separated by a space. Either
can be omitted, or both. Examples:
Graphics.open_graph "foo:0" connects to the display foo:0 and creates a
window with the default geometry
Graphics.open_graph "foo:0 300x100+50-0" connects to the display foo:0 and
creates a window 300 pixels wide by 100 pixels tall, at location (50,0)
Graphics.open_graph " 300x100+50-0" connects to the default display and
creates a window 300 pixels wide by 100 pixels tall, at location (50,0)
Graphics.open_graph "" connects to the default display and creates a
window with the default geometry.

Put a 'space' in the argument to get the window you want (should be 200 for your cardioide):
let () = open_graph " 300x200"

I met the same problem and it was because I used the Windows subsystem for linux(WSL) so it needs a XServer to run graphical application. And the Ubuntu Wiki For WSL helped solve the problem. I downloaded and installed MobaXterm (it has free community version!) and it automatically detects the WSL and runs it inside the app. Try the same code before and a graphical window will pop up!

Related

What is the argument for Graphics.set_font from the OCaml Graphics package?

I'm trying to use the Ocaml Graphics package. I want to create a GUI for my chat server application. My code is:
let window = Graphics.open_graph "";
Graphics.set_window_title "caml-chat";
Graphics.set_font "ubuntu";
Graphics.set_text_size 12;
Graphics.draw_string "hello!"
However, Graphics.set_font "ubuntu" does not work. The documentation says that the string argument is system dependent, but I cannot find any more information than that. The only mention I found was in the answers to this question, and it didn't work.
Does anyone know anything else about setting the font? (Or can point me in the direction of a simple graphics library with better documentation?)
Although you didn't specify your system, I will assume that it is Linux (I doubt that Windows has an ubuntu font).
On Linux, the set_font function passes the argument to the X Lib's XLoadFont function. You can use the fc-list or xfontsel utilities to query for the available fonts on your system, or call directly to the XListFonts function.
For example,
fc-list | cut -d: -f2 | sort -u
will give you a list of font families, which you can pass to set_font function. Some lines will have more than one family per line, separated with comman (,). There are many more options, you can specify various styles, sizes, etc. But this is all out of the scope. You can the fontconfig guide to learn more about the font subsystem. For example, [here], at the section "Font Names", you can find the explanation of how the font name is constructed.

sdl ttf_rendertext_blended fails randomly

EDIT: Even that the problem still exists, I haven't been able to reproduce this frequently enough to examine it closer. See more info at the end of the question.
I started to develop a game, and I am currently writing basic library for it. I'm using D programming language with SDL-2 and OpenGL 3 (using Derelict3 bindings), on Linux Mint 13 (Maya). Compiler is DMD64 D Compiler v2.067.1, and I rebuild binary each time with 'rdmd'.
To render (changing) text, I create glyphs on-demand. The piece of code I use for this is:
class Font {
...
Texture render(char c) {
if(!(c in rendered)) rendered[c] = texture(to!string(c));
return rendered[c];
}
Texture texture(string text) {
SDL_Color color={255, 255, 255, 255};
auto bitmap = TTF_RenderText_Blended(
font,
std.string.toStringz(text),
color
);
if(!bitmap) {
throw new TTFError(
"TTF_RenderText_Blended: " ~
to!string(TTF_GetError()) ~ ": '" ~ text ~ "'"
);
}
auto texture = new Texture(bitmap);
SDL_FreeSurface(bitmap);
return texture;
}
The problem is that this fails purely randomly. Sometimes it works without any problems. When it fails to render a glyph, it is interesting that it will fail to render the same glyph over and over again. Here is an example when catching the exception I throw:
...
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
TTF_RenderText_Blended: Text has zero width: '9'
TTF_RenderText_Blended: Text has zero width: '6'
...
(I'm printing score to screen, other numbers showing fine except those few ones). The numbers TTF_RenderText_Blended fails to render vary from run to run, and as mentioned, time to time it renders all the numbers.
One detail is that static strings I render before entering game loop have not yet failed to render, just single letters I use for changing texts.
I'm pretty much out of any ideas, and haven't found anything related to this problem by searching web. Any ideas to look for solutions are very well appreciated.
CURRENT SITUATION: I updated compiler to DMD 2.067.1 and the problem remains (compilers used so far: 2.066.1, 2.067.1). The whole - lets say project family is in the github at the moment:
https://github.com/mkoskim/games
The text glyph rendering function is located in this file:
https://github.com/mkoskim/games/blob/master/engine/ext/font.d
...and it is used from here:
https://github.com/mkoskim/games/blob/master/engine/ext/gui/label.d
The problem occurs mainly/most frequently in the pacman game (although very seldomly just right now):
https://github.com/mkoskim/games/tree/master/testbench/pacman
If you want to try it out, first, read the (hopefully complete enough) installation instructions:
https://github.com/mkoskim/games/blob/master/INSTALL
The project is made for 64-bit Linux Mint Maya, and it is currently not that user friendly and portable from building perspective. Pacman is the only demo that (hopefully) works without game controller. After successful installation of required libraries and tools, you can build it with command:
games/testbench/pacman$ make default
I ran into the exact same issue, and for me it was fixed by keeping the SDL_RWops structure used to create the font (with TTF_OpenFontRW) alive for the whole lifetime of the TTF_Font created by it. I saw you're creating the font with TTF_OpenFontRW as well so I assume this will fix it for you as well. It looks like SDL_ttf relies on this being kept alive, it reads freed memory otherwise.
I know this question is a little bit outdated but I maybe had a similar problem.
I fixed it with simply call SDL_DestroyTexture() every time befor I used TTF_Render_Text_Blended() :)

SDL_RenderCopy() has strange behavior on Raspberry PI

This is driving me up the wall..
I've got a very simple SDL2 program.
It has a array of 3 SDL_Texture pointers.
These textures are filled as follows:
SDL_Texture *myarray[15];
SDL_Surface *surface;
for(int i=0;i<3;i++)
{
char filename[] = "X.bmp";
filename[0] = i + '0';
surface = SDL_LoadBMP(filename);
myarray[i] = SDL_CreateTextureFromSurface(myrenderer,surface);
SDL_FreeSurface(surface);
}
This works, no errors.
In the main loop (which is just a standard event loop waiting for SDL_QUIT, keystrokes and a user-event which a SDL_Timer puts in the event queue every second) I just do (for the timer triggered event):
idx = (idx+1) % 3; // idx is global var initially 0.
SDL_RenderClear(myrenderer);
SDL_RenderCopy(myrenderer, myarray[idx], NULL, NULL);
SDL_RendererPresent(myrenderer);
This works fine for 0.bmp and 1.bmp, but the 3rd image (2.bmp) simply shows as a black field.
This is structural.
If I alternate the first 2 images they are both fine.
If I alternate the 2nd and 3rd image the 3rd image doesn't show.
If I use more than 3 images then 3 and upwards show as black.
Loading order doesn't matter. It starts going wrong with the 3rd image loaded from disk.
All images are properly formatted BMP's.
I even saved 2.bmp back to disk under a different name by using SDL_SaveBMP() after it was loaded to make sure it got loaded in memory OK. The new file is bit for bit identical to the original.
This program, without modifications and the same bmp files, works fine on OSX (XCode5) and Windows (VC++ 2012 Express).
The problem only shows on the Raspberry PI.
I have placed explicit error checks on every call that can leave a result/error-code (not shown in the samples above for brevity) but all of them show "no error".
I have used the latest stable source set of www.libsdl.org and compiled as instructed (configure, make, make install, etc.).
Anybody got any idea what could be going on ?
P.S.
Keyboard input doesn't seem to work either on my PI, but I haven't delved into that yet.
Answering myself as I finally figured it out myself...
I finally went back to the README-raspberrypi.txt that came with the SDL2 sources.
I didn't read it carefully enough the first time around...
Problem 1: I'am running on a FULL-HD display. The PI's default GPU memory is 64MB which is not enough for large displays and double-buffering. As suggested in the README I increased this to 128MB and this solved the black image problem.
Problem 2: Text input wasn't working because my user-account was not in the input group. I had added the default "pi" account to the input group initially, but when I later started using another account I forgot to add that user to the group.
In short: Caught by my own (too) quick skimming of the documentation.

Boost Program Options: Description too wide for terminal

I am using Boost Program Options to parse command line arguments (and I don't want to miss it since it works great). However, I have one problem: Boost program options offer the possibility to assign a description to each option. Boost then offers the possibility to
cout << program_options_description << endl
to nicely display help explaining the options. However, it seems to be the case that these error messages are adjusted to a terminal width of 80 (I conclude this from the fact that for a width of 80, the line breaks are nicely set).
If my current terminal has another width (in particular one that has less than 80 columns), the displayed help looks very unnatural due to automatic line breaks done by the terminal.
So: Is there a possibility that Boost automatically adjusts the option descriptions to the current terminal width?
The options_description accepts the column width as a parameter.
options_description(const std::string &, unsigned = m_default_line_length,
unsigned = m_default_line_length/2);
I believe the default is 80 for m_default_line_length. Also, see this SO question to get the terminal width on linux and then pass that to the constructor. Or if you are windows, you would want to call GetConsoleScreenBufferInfo.

Write information into empty window with c++

So I'm building this game engine thinggy, and found it to be VERY hard to create some kind of an overlay with debug information into the main game window with D3D11 or at all draw text, so I thought I'd create an other window to contain my debug data.
I got the window created fine and all, but I have no idea how to write my debug info into it. I do not want to use the windows form designer as that would have to convert my project into a CLR project which I do not want.
I have been googling now for 3 hours at least (honest) and tried various solutions but none of them really seemed practical to use/they were not working.
The debug info I'd like to write originates from global float values. An example would be CAM_POS_X which holds a floating point value which indicates at which X co ordinate the camera is currently at.
Something like this is desired:
|SiriusAlpha 0.1 Debug window_ |
|Current X position: CAM_POS_X|
|Current Y position: CAM_POS_Y|
|Current Z position: CAM_POS_Z|
|Current YAW: CAM_YAW______|
|Current PITCH: CAM_PITCH___|
|Current FPS: CUR_FPS_______|
All of these values are not nescessarily floating point variables. They could be strings, doubles, integers or even booleans.
If anyone would be willing to explain to me how to do this in D3D11 and I could skip the whole debug window schenennigans I'd be even happier.
Otherways, I'd be delighted if somebody could explain to me how this is done.
Have you tried TextOut()? Read the article on msdn. You should already have a device context, the rest is quick and easy.
The TextOut function writes a character string at the specified location, using the currently selected font, background color, and text color.
Printing to a string is trivial.
wchar buf[128];
swprintf(buf, "Current X Position: %f", CAM_X_POS);
TextOut(yourDC, screenXPos, screenYPos, &buf, sizeof(buf));
I haven't tested this, but from the MSDN documentation this should work fine.