Process ends at TTF_RenderText_Shaded line - c

My SDL program contains:
TTF_Init();
TTF_Font *font = TTF_OpenFont("segoeui.ttf",13);
SDL_Color textColor = {0,0,0};
SDL_Color backgroundColor = {34,177,76};
SDL_Surface *myText = TTF_RenderText_Shaded(font,"Some text",textColor,backgroundColor);
When I run the program from the Build and run button in Code::Blocks, there isn't any problem but when I run the program from the folder in Windows Explorer, the window opens and closes directly, and after the window closes, the process isn't running any more and the files stderr.txt and stdout.txt are still there. I've made some tests and found out that it's the line SDL_Surface *myText = TTF_RenderText_Shaded(font,"Some text",textColor,backgroundColor); that seems to end the process just like that like if the End Process button would have been pressed in the task manager.
Why does it do that? How can I fix it?

You should set your font with absolute path and not a relative one. If you plan to do crossplateform deployment, you may want to include something like that:
TTF_Font *font;
#ifdef _WIN32
font = TTF_OpenFont("WinPath",13); // The windows path
#elif linux
font = TTF_OpenFont("LinuxPaht",13); // The linux path
#elif MacOS
font = TTF_OpenFont("Mac path",13); // The mac path
#endif
if(font == null)
// Throw an error, return or whatever.
You can get exact directives at Detect Windows or Linux in C, C++

Related

Can not load personal font using XLoadFont

Hi I am new to X11 and I'm trying to load a font from a directory using XLoadFont and then printing some text with XDrawText but for some reason, I can't load the font (.ttf format) where I load the font:
static void setFont(
Object *_this,
const char *path)
{
mc_textPr *this = _this;
Display *display = getDisplay();
Font tmp = XLoadFont(display, path);
if (!tmp) {
raise("Invalid path to font\n");
return;
}
unloadFont(this->text.font, display);
this->text.font = tmp;
}
Before you, tell me that X11 is hard and should use something else it is for a school project which requires the use of X11.
I also know about this to resources
http://xopendisplay.hilltopia.ca/2009/Feb/Xlib-tutorial-part-4----Text.html
https://tronche.com/gui/x/
but I can't find why it freaks out.
here is the actual Error :
X Error of failed request: BadName (named color or font does not exist)
Major opcode of failed request: 45 (X_OpenFont)
Serial number of failed request: 17
Current serial number in output stream: 27
XLoadFont() can only load X11 bitmap fonts which are already available to the X server. It cannot load TTF font files, and it cannot load fonts from files you specify.
To find the names of fonts that are available on your system, run xfontsel.
Virtually all modern software uses external libraries, like Cairo, to draw text. X11 fonts are only used by a few very old applications, like xterm.

CLion won't show output in Debug

When I start up the program, this is the output:
-------------------- HASHMAP MANAGEMENT BOOT MENU -------------------------
Would you like to:
(a) create a new hashmap
(b) load an existing one
(q) exit
>
However, when debugging, none of this shows up. Checking the debug, it does go over the printf() commands, but it just refuses to let them show up in the console. Input registers, but output never comes.
int main(void){
bool on = true;
char choice = ' ';
int status = 0;
while(on){
if(status == -1){
printf("\n[ERROR] : HASHMAP NOT INITIALISED\n");
}
printf("\n-------------------- HASHMAP MANAGEMENT BOOT MENU -------------------------\n");
printf("Would you like to:\n(a) create a new hashmap\n(b) load an existing one\n(q) exit\n> ");
scanf("%c",&choice);
...
...
}
}
This is how the start of the code is, excluding all the #includes. Also, for some reason, CLion says the code I'm building is task2-a.c | Debug if that's any help. task2-a.c being the name of the C file that's being built. I dunno what's going on...
Update: Debugging works great on Ubuntu 17.04 Clion 2017.2. It just doesn't work on Windows 10 CLion 2017.3.
Putting setbuf(stdout, 0); before any printf statement or any output happens fixed this problem.
If you don't care to use the built-in clion console, you can solve the issue by changing the default debugger used by clion.
Under Settings => Toolchain => <your compiler> => Debugger change Bundled GDB to your compiler's debugger, e.g. MinGW:

How to use Notepad++ as editor for 7zip, without showing a console window?

This question could be generalized as: How to launch a given process with any intermediate process spawned having the same lifetime as the launched process, and without showing a console window. Specific arguments must be passed to the final process, but no arguments can be passed in the initial call.
To use Notepad++ as editor for 7zip you would need to launch notepad++.exe with the -multiInst command line parameter, otherwise it instantly closes and forwards arguments to the existing instance. Since 7zip picks up the changes you did to its temp file when the invoked program closes, you never get a chance to edit it.
Problem is, 7zip doesn't allow you to enter arguments for whatever program you're configuring as editor.
Obvious solutions that don't work, already tried:
Call a batch file, but then I'm stuck with an unsightly (and easy to close accidentally) console window for the duration of the edition - not acceptable.
Call a batch file which uses start to call Notepad++ : the console window does close, but unfortunately the batch executor process which is what Notepad++ was watching is gone, so it thinks you're already done editing, i.e. back to the initial problem.
Use wscript, which doesn't show a console window. Tracking the process lifetime is complex however (Wait for program to complete) and it makes you rely on old tech in maintenance mode that has a malware connotation.
How would you go about this? No solution I've tried myself or read about has been fully satisfying.
Note: this is not exactly the same question as Execute Batch File without Command line visible since this has the added requirement that whatever launcher used must stay open for the whole lifetime of the launched process, and that you can't pass command line arguments to the launcher.
I ended up writing my own utility which I'm tentatively calling NoConsoleProgramLauncher to serve as an intermediate between 7z and Notepad++. It's rough first draft code, but I thought sharing it might still be useful since this question has been without answer for three years.
#include <fstream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <filesystem>
HINSTANCE hInst;
void launchProcess(std::wstring commandLine);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//Get current executable's location
HMODULE hModule = GetModuleHandleW(NULL);
WCHAR executableFolder[MAX_PATH];
GetModuleFileNameW(hModule, executableFolder, MAX_PATH);
std::experimental::filesystem::v1::path path(executableFolder);
path.remove_filename();
path.append(L"NoConsoleProgramLauncher_Arguments.txt");
std::wifstream infile(path);
std::wstring commandLine;
std::getline(infile, commandLine);
commandLine += L" ";
commandLine += lpCmdLine;
launchProcess(commandLine);
}
void launchProcess(std::wstring commandLine)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
&commandLine[0], // Command line - C++ 11 guarantees that string's internal buffer is contiguous and null-terminated.
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
Basically, if you've done a minimum of C++ coding before, it's a matter of pasting this into a new Visual Studio 2017 Windows Desktop Application project, fixing up the includes if needed, and building.
As you can see in the source code, when launched the executable looks for a file in the same folder as itself named "NoConsoleProgramLauncher_Arguments.txt", and calls the command line it finds in there. As specified in the question, no console window will be shown, and the program will wait for the spawned process to terminate before exiting, so 7zip keeps waiting to pick up the changes.
This is what I put in my NoConsoleProgramLauncher_Arguments.txt file:
"C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst -nosession
And in the 7zip configuration, I've set the editor to point to my NoConsoleProgramLauncher.exe program.
The real solution would of course be to gently pester the 7z authors about this, or better, submit a pull request to 7z to implement passing arguments to your editor of choice.

GTK+/GCC Crashing On Start

I'm trying to learn how to use GTK in C.
I'm developing using Eclipse CDT. I copied the following code from an examples website:
#include <gtk/gtk.h>
#include <stdlib.h>
void displayUI()
{
GtkWidget* mainWindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(mainWindow), 400, 300);
gtk_window_set_title(GTK_WINDOW(mainWindow), "GTK Simple Example");
gtk_window_set_position(GTK_WINDOW(mainWindow), GTK_WIN_POS_CENTER_ALWAYS);
gtk_signal_connect(GTK_OBJECT(mainWindow), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(mainWindow);
}
int main(int argc, char *argv[]) {
gboolean b = gtk_init_check(&argc, &argv);
if(b == 0) {
puts("Failed to init");
exit(0);
}
gtk_init(&argc, &argv);
displayUI();
gtk_main();
return EXIT_SUCCESS;
}
Every time I try to run this program (or anything else involving GTK, Windows immediately displays a "InsertProgramNameHere.exe has crashed" message.
I have MinGW, MinSYS, PKG-CONFIG and MAKE all properly installed. The program compiles fine... it just won't run.
Any ideas?
UPDATE
I found this error log. There seems to be a dependency problem.
Faulting application TestRun.exe, version 0.0.0.0, time stamp 0x4f839a6a, faulting
module libgtk-win32-2.0-0.dll, version 6.0.6002.18541, time stamp 0x4ec3e39f, exception
code 0xc0000135, fault offset 0x0006f52f, process id 0x1674, application start time
0x01cd16c174d3df90.
exception code 0xc0000135
That is STATUS_DLL_NOT_FOUND. Gtk+ has a large number of dependent DLLs. Probably your best bet to get started is to use the all-in-one bundle and copy the entire content of the bin directory in the archive to your program's EXE directory. Crude but the docs are quite unapologetic about it:
Many of the developer files are relatively irrelevant. If you intend to redistribute the GTK+ run-time, you need to figure out which files you can leave out yourself
Are you sure your GTK installation directory is in your PATH ?
You need to add the path to (MinGW, MinSYS and gtk-dev)'s bin folder in the environment variables.

How Do I Compile a C Program to Run Without a Command Box?

I need a very simple program to run on any version of Windows, let's say >= Win 98, without requiring any pre-installed framework like dotnet. I thought C would be a great idea to do this.
The program should start a process from the parent directory by using a system command.
Start C program (invisible) > program starts process > program exits
This is how it looks:
#include <stdio.h>
#include <stdlib.h>
int main() {
system("..\\someprogram.exe");
return 0;
}
I call this program from a Flash projector, which only allows to start programs in a specific subfolder "fscommand" – but I have to start a process located in the same directory as the projector.
Anyway, it works fine! But the C program opens a command box, then starts the process and leaves the command box open as long as the process runs. So here is how it should work, in order how i would appreciate it:
Do not open a command box at all (I'd like that, really ;)
Both 3) and 4)
Close the command box after starting the process (exit the C program)
Open the command box minimized by default
I can't change any Windows settings for the C executable or use a shortcut, as this will run directly from a CD later.
I use Open Watcom to compile my program. Both image types (target options) that produce an executable (Character-mode Executable / Windowed Executable) have the same result.
I did a google search and found http://www.ntwind.com/software/utilities/hstart.html
Your using a console app, you could change it to a windows app using winmain()
You can use a shortcut to a file in the same folder, not sure why your discounting that method.
start will give you a fork so your intermediate app can close - not sure about win98 tho.
system("start ..\\someprogram.exe");
Instead of system you can use createProcess to launch the app, theis will avoid the system commands console.
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( "..\\someprogram.exe", // module name
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
// Wait until child process exits. In your case you don't care to wait anyway
// WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
The console window shows up because you built your program as a console application. I don't know how to avoid that in C, but in Delphi is was a simple {$Console Off} pragma in the project file.
GCC has a command line option -mwindows, which I think achieves the same, so you could search into this direction.
I think the _exec and/or _spawn functions do what you need, though I'm not sure.
If not, you can always use CreateProcess, though it can be a little more tedious in some ways.
You could (for example) use hstart instead of your own program to start that exe.
(This would result in no black box at all.)
CreateProcess with CREATE_NO_WINDOW flag is what you want, but I want to add something. To support also cmd style commands (such as DIR, SET, ... ) which have no executables and can't be passed to CreateProcess alone, you should call cmd.exe /C someprogram where someprogram is name of executable, bat file, or command.
A friend came up with a completely different solution. Now I use AutoIt with a short compiled script to start the process. This is very simple and the launcher process is completely invisible. :)
filename = ..\someprogram.exe
if FileExist(filename) {
Run, open %filename%
}

Resources