Icon for executable and window

Monkey Targets Forums/Desktop/Icon for executable and window

Rus(Posted 2011) [#1]
I've been exploring the GLFW target for Monkey and I'm trying to add an icon to the generated executable. I haven't managed to find a value in any configuration file that points to an "icon.ico" or similar. I did have some success by injecting an icon via Resource Hacker. This gets an icon on the file but doesn't get an icon to display at the top left of the window. I'd like to avoid doing it this way if possible since it is a hack.

Has anyone explored these issues?

Do share what you know, as I'd really like the final polish of an icon on my executable, especially for potentially commercial stuff. Thanks!


MikeHart(Posted 2011) [#2]
Did you had a look into the BUILD/GLFW folder?


Rus(Posted 2011) [#3]
Yup. Looked into every file, just in case some icon stuff was hidden in something that was not clearly marked as a config file, but I couldn't find anything. Does this require some external non-Monkey code? I'd appreciate any help I can get with that, as it's not something I'm good at.


dave.h(Posted 2011) [#4]
if you need an icon displayed on your desktop when you redistribute your game you could use inno setup.It packs your program so you can redistribute it and allows for an icon to be added.so for instance if you were to have a link on your website to the packed game when they ran the install routine it will intall onto your pc where youve told it and place an icon on your desktop.I used this for c++ programs and its free.

http://www.jrsoftware.org/isinfo.php

there are videos on youtube on how to use it and they are easy to follow.Never tried it with a monkeys game though.


DruggedBunny(Posted 2011) [#5]
I don't know enough to write this in C and import it, but if you use Resource Hacker to give your program an icon, these Win32 API calls (shown in PureBasic code) will do it:

; Get app's filename...

filename$ = Space (#MAX_PATH) ; PB hack here, just need a buffer MAX_PATH (260) bytes in size to account for any valid path...
GetModuleFileName_ (#Null, @filename$, #MAX_PATH) ; @filename$ means pointer to filename$...

; Get icon from executable file and set window icon...

icon = ExtractIcon_ (GetModuleHandle_ (#Null), @filename$, 0)
SetClassLong_ (WindowID (), #GCL_HICON, icon)


Anyone out there handy at interfacing with external code and can express these OS calls (starting with underscores above) in C?


DruggedBunny(Posted 2011) [#6]
Whoa, I actually figured it out! Note that it won't have any effect until you add an icon to your executable via Resource Hacker.

Save this as "icon.cpp" or whatever you prefer:

#include <cstdio>
#include <windows.h>
#include <shellapi.h>

void setglfwicon ()
{
	TCHAR filename [MAX_PATH];

	int chars = 0;

	chars = GetModuleFileName (NULL, filename, MAX_PATH);

	HICON icon = ExtractIcon (GetModuleHandle (NULL), filename, 0);

	if (icon != NULL)
	{
		HWND window = FindWindow (NULL, "Monkey Game");

		if (window)
		{
			SetClassLong (window, GCL_HICON, (LONG)icon);
		}
	}

}


(Any C/C++ coders out there giggling, I'm not a C/C++ coder!)

In your Monkey program, you need to import this and add the Extern section below:

Import mojo ' As normal...

Import "windowicon.cpp"

' Any other imports...

Extern
	Function UpdateWindowIcon () = "setglfwicon"
Public


... and finally, call UpdateWindowIcon on startup (but only for GLFW). Seems to be fine if called as the first item in Main -- just as long as the window is open:

Function Main ()

	' UpdateWindowIcon reads the GLFW executable's icon and sets the titlebar icon from it...

	#If TARGET = "glfw"
		UpdateWindowIcon
	#Endif
	
	New GameApp
End


Note that the CPP code is looking for a window called "Monkey Game" -- you'll have to edit this if you've renamed your GLFW window. There are smarter ways to do it, but I've stretched my CPP limits for the time being (actually, I think I've just used plain C, haven't I?)...

Anyway, it was at least a good little learning experience for interfacing with native code.


dawlane(Posted 2012) [#7]
Ok I know this is late but you can actually use Visual Studio to do this for you on once you have the code for your program.

First make sure you have your icon ready (read about windows icons to make sure that they comply plus there's an icon editor called Greefish Icon Editor) as a .ico and put it in your projects build\glfw\vc2010 folder.

You can use Visual Studio to make your icons too or add an existing one. To do this, add one to your project by the menu (Project->Add New Item). In the dialog box in the left pane select Resources and in the main panel select Icon File (.ico) and give it a name.

The Icon editor will have a panel on the left for the size of images of the icon. Right click on this pane and select New Image Type from the context menu to select the size and bit depth.
You can copy and paste from an external program like Photoshop but make sure the image you copy is flattened as well as the right size and bit depth.

Second add a resource file to the project (Project->Add New Item). In the dialog box in the left pane select Resource and in the main panel select Resource File(.rc) and give it a name say icon.

Now we need the Resource View (View->Other Windows or Ctrl+W then R).
Right click the icon.rc folder in the Resource View and select Add Resource in the context menu.

In the dialog shown press the Custom button and in the dialog for New Custom Resource type GLFW.

In the Solution Explorer right click the icon.rc and select View Code to see the code for the .rc file.

Look for the comments for GLFW and add GLFW_ICON ICON "icon.ico" after them.

Then open up the main.cpp and add #include "../../../vc2010/resource.h" to the top of the file.

Then it's just a case of compiling from Visual studio.

Update: You can also open the MonkeyGame.sln in the targets directory and just add a resource file, add the GLFW_ICON ICON to it as above (no need to add any thing to the main.cpp file). This way means that all you then have to do is drop an icon file in the vc2010 build folder open up the project in VS and rebuild.


Soap(Posted 2014) [#8]
Rezzing this because it would be a nice bit of polish if monkey had a default icon already in the project files which we could replace / be able to define custom icon files which would be put in the right place. This goes for xcode desktop builds too. Add the right line to Info.plist "CFBundleIconFile" value string "icon" and include a default monkey head icon.icns in the in the "resources/macos" folder.

I should probably put my money where my mouth is and put up git pull requests for changes like this. :p