Using some extensions with Dev-C++

January 2018, Roberto Foschini (rfoschini@itisravenna.it)

Using the WinBGIm Graphics Library with Dev-C++

You need to include and to link library. And use the Windows GDI Functions.

Description:

This page provides a mechanism for using the WinBGIm Graphics Library with the Dev-C++ development environment. Additional documentation for the WinBGIm graphics library is available at www.cs.colorado.edu/~main/bgi.

Installation notes:

  1. Install Dev-C++. I installed from the Version 5.11 Setup File.
  2. Download graphics.h to the …/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/ subdirectory of the Dev-C++ directories. (or ?!?)
  3. Download libbgi.a to the …/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32/ subdirectory of the Dev-C++ directories. (or ?!?)
  4. Download  BGI.zip or  BGI.zip and extract files in the same directories specified above.

Compile and run notes:

  1. Edit programs that use the WinBGIm graphics library, such as this one:
    #include <math.h>
    #include <graphics.h>
    
    int main() {
    	const unsigned XMAX = 1200, YMAX = 500;
    
    	initwindow(XMAX, YMAX, "First Sample");
    	printf("window: %u %u\n", getmaxx(), getmaxy());
    
    	circle(100, 50, 40);
    	line(0, 0, getmaxx(), getmaxy());
    	putpixel(10, 20, GREEN);
    	rectangle(0, 0, getmaxx(), getmaxy());
    	ellipse(200, 200, 90, 180, 100, 50);
    
    	while (!kbhit())
    		delay(200);
    	closegraph();
    
    	return 0;
    }
    
    or download one of these:  TestGraphics.cpp  TestWinbgim.cpp  graphic.cpp  MandelbrotSet.cpp  traiettoria proiettile.cpp  CurveHilbert.cpp  CurveSierpinski.cpp  Koch Snowflake.cpp  JuliaSet.cpp  DisplayBar.cpp  HTree.cpp  ArchimedeanSpiral.cpp
  2. As a target, select "TDM-GCC 4.9.2 ..."
  3. Instruct the linker to link the following libraries. From Tools → Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
    -lbgi
    -lgdi32
    -lcomdlg32
    -luuid
    -loleaut32
    -lole32
  4. Compile and run programs normally. Have fun!

Free notes:

  1. This library (libbgi.a) miss some functions (f.i.: setcurrentwindow()).
  2. Linking with a "TDM-GCC 4.9.2 64-bit ..." target while libbgi.a is in …/Dev-Cpp/MinGW64/lib/ results in "skipping incompatible libbgi.a when searching for -lbgi"

Using SFML with Dev-C++

This section illustrates how to include the SFML library to draw various geometric figures on the screen, but you can also develop 2D and 3D games.

Download all the library:

  1. Install Dev-C++. I installed from Version 5.11 Setup File.
  2. Download SFML from SFML 2.4.2
  3. Extract the .zip and move the extracted folder to C:/(for Windows)

Installation notes:

  1. Go to Tools > Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
  2. -lsfml-audio
    -lsfml-window
    -lsfml-graphics

  3. Go to Directories > Libraries and add the lib folder from the SFML folder.
  4. Go to Directories > C++ Includes and add the include folder from the SFML folder.

Creation of the project:

  1. Create an empty c++ project.
  2. Copy all the .dll from the SFML bin folder to the folder of the project.
  3. If you have done all these steps you will be able to run the program.

Example code:

#include <SFML/Graphics.hpp>

// if you want to remove the 'sf::', uncomment this line and remove all the 'sf::'.
// using namespace sf;

int main() {
	sf::RenderWindow window(sf::VideoMode(200, 200), "SFML window");
	sf::CircleShape circleShape(50.0f);
	circleShape.setFillColor(sf::Color(100, 250, 50));

	if(window.isOpen()) {
		sf::Event event;
		while(window.pollEvent(event)) {
			if(event.type == sf::Event::Closed)
				window.close();
		}
		window.clear();
		window.draw(circleShape);
		window.display();
	}
	return 0;
}

Other resources:

Using ConsoleIO functions with Dev-C++

You need to include and to link library. And use the Windows Console Functions

Installation notes:

  1. Download altconio.h or wconio.h or devcpp-conio.h(see zip) or conio2.h(see zip) to the …/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/ subdirectory of the Dev-C++ directories. (or ?!?)
  2. Download libconio.a or libconio.a to the …/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32/ subdirectory of the Dev-C++ directories. (or ?!?)
  3. Download  devcpp-conio20.zip  devcpp-conio21.zip and extract files in the same directories specified above.

Compile and run notes:

  1. Edit programs that use ConsoleIO functions, such as this one:
    #include <stdio.h>
    #include <conio.h>
    #include "coniow.h"
    
    int main(int argc, char** argv) {
    	clrscr();
    	gotoxy(10, 20);
    
    	textbackground(YELLOW);
    	printf("     aaaaa     \n");
    
    	textcolor(GREEN);
    	printf("ciao");
    
    	textcolor(RED);
    	printf("%i %i", wherex(), wherey());
    
    	textbackground(CYAN);
    	printf("     bbbbb     \n");
    
    	textcolor(LIGHTGRAY);
    	textbackground(BLACK);
    
    	highvideo();
    	printf("highvideo\n");
    	lowvideo();
    	printf("lowvideo\n");
    	normvideo();
    	printf("normvideo\n");
    
    	_setcursortype(_NOCURSOR);
    	_setcursortype(_NORMALCURSOR);
    
    	return 0;
    }
    
    
  2. Instruct the linker to link the following libraries. From Tools → Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
    -laltconio
  3. Compile and run programs normally. Have fun!
Note: For who needs gotoxy() function only, this is a possible implementation: coniow.h coniow.c coniou.h coniou.c coniowexample.c devcpp-colors.c
#include <stdio.h>

#ifdef _WIN32 || _WIN64
#include <windows.h>

typedef enum {
	BLACK = 0,
	BLUE = 1,
	GREEN = 2,
	CYAN = 3,
	RED = 4,
	MAGENTA = 5,
	BROWN = 6,
	LIGHTGRAY = 7,
	DARKGRAY = 8,
	LIGHTBLUE = 9,
	LIGHTGREEN = 10,
	LIGHTCYAN = 11,
	LIGHTRED = 12,
	LIGHTMAGENTA = 13,
	YELLOW = 14,
	WHITE = 15,
} COLORS;

// posiziona il cursore a riga 'r' (base 1) e colonna 'c' (base 1)
void gotoxy(short c, short r) {
	if (c>0 && r>0) {
		COORD CursorPosition = { c-1, r-1 };
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleCursorPosition(hOut, CursorPosition);
	}
}
void textbackground(short color) {
	if(color>=0) {
		CONSOLE_SCREEN_BUFFER_INFO csbi;
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		GetConsoleScreenBufferInfo(hOut, &csbi);
		csbi.wAttributes &= 0xFF0F;
		csbi.wAttributes |= (color & 0xF)<<4;
		SetConsoleTextAttribute(hOut, csbi.wAttributes);
	}
}
void textcolor(short color) {
	if(color>=0) {
		CONSOLE_SCREEN_BUFFER_INFO csbi;
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		GetConsoleScreenBufferInfo(hOut, &csbi);
		csbi.wAttributes &= 0xFFF0;
		csbi.wAttributes |= color & 0xF;
		SetConsoleTextAttribute(hOut, csbi.wAttributes);
	}
}
void printcolor(const char * str, COLORS Text, COLORS Background) {
	if(color>=0) {
		CONSOLE_SCREEN_BUFFER_INFO csbi;
		HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
		GetConsoleScreenBufferInfo(hOut, &csbi);
		SetConsoleTextAttribute(hOut, (Text & 0xF) | ((Background & 0xF)<<4));
		printf("%s", str);
		SetConsoleTextAttribute(hOut, csbi.wAttributes);
	}
}

#else
typedef enum {
	BLACK = 0,
	RED = 1,
	GREEN = 2,
	YELLOW = 3,
	BLUE = 4,
	MAGENTA = 5,
	CYAN = 6,
	WHITE = 7,
	DEFAULTCOLOR = 9,
} COLORS;
// posiziona il cursore a riga 'r' (base 1) e colonna 'c' (base 1)
void gotoxy(short c, short r) {	// posiziona il cursore a riga 'r' (base 1) e colonna 'c' (base 1)
	if (c>0 && r>0)
		printf("\033[%i;%if", r, c);
}
void textbackground(short color) {
	if(color>=0 && color<=DEFAULTCOLOR)
		printf("\033[0;%im", 40+color);
}
void textcolor(short color) {
	if(color>=0 && color<=DEFAULTCOLOR)
		printf("\033[0;%im", 30+color);
}

#endif

int main() {
	clrscr();
	for (unsigned i=0; i!=10; ++i) {
		gotoxy(1+i, 1+2*i);
		switch (i%10) {
		case 0:
			textattr(NORMAL);
			break;
		case 1:
			textattr(BRIGHT);
			break;
		case 2:
			textattr(UNDERLINE);
			break;
		case 3:
			textattr(BLINK);
			break;
		case 4:
			textattr(REVERSE);
			break;
		case 5:
			textcolor(RED);
			break;
		case 6:
			textcolor(GREEN);
			break;
		case 7:
			textcolor(BLUE);
			break;
		case 8:
			textcolor(YELLOW);
			break;
		case 9:
			textcolor(WHITE);
			break;
		default:
			break;
		}
		printf("***");
	}
	return 0;
}

Using DMX512 with Dev-C++

You need to include and to link library. And use the Windows Communications Functions. (sample implementation: dmxlibw.h, dmxlibl.h, dmxlibsim.h)

Remember to install FTDI driver

Compile and run notes:

  1. Edit programs that use DMX functions, such as this one:
    #include <stdio.h>
    #include <stdbool.h>
    #include "dmxlibw.h"
    #define MAXCHANNEL 8
    int main()
    {
    	const bool terminated = false;
    	const int inter_frame_delay = 1000;
    	
    //	unsigned char dmxRx[MAXCHANNEL];
    	unsigned char dmxTx[][MAXCHANNEL]={
    		{31, 127,0,0,0,0,0},
    		{31, 0,127,0,0,0,0},
    		{31, 0,0,127,0,0,0},
    		{31, 0,0,0,127,0,0},
    //		{31, 0,0,0,0,127,0},
    //		{31, 0,0,0,0,0,127},
    	};
    	unsigned int NbFramesSent = 0;
    	HANDLE dmxHandle = dmxopen("\\\\.\\COM13");
    	if (dmxHandle == INVALID_HANDLE_VALUE) {
    		printf("No device - Error");
    		return 1;
    	}
    
    	{
    		// Skeleton per MIDI
    		unsigned char dmxTx[MAXCHANNEL];
    		unsigned char key = rand()%128;
    		unsigned char velocity = rand()%128;
    		dmxTx[0] = 127;					// Master
    		dmxTx[1] = (key==35||key==36) ? velocity*2 : 0;	// Red (Bass drum)
    		dmxTx[2] = (key==38||key==40) ? velocity*2 : 0;	// Green (Snare drum)
    		dmxTx[3] = (key==51||key==59) ? velocity*2 : 0;	// Blue (Ride cymbal)
    		dmxTx[4] = (key==49||key==57) ? velocity*2 : 0;	// White (Crash cymbal)
    		dmxwrite(dmxHandle, sizeof(dmxTx), dmxTx);
    		Sleep(500);
    	}
    
    	size_t k = 0;
    	while (!terminated) {
    		printf("Universe %3d:", dmxHandle);
    		for(size_t j=0; j!=MAXCHANNEL; ++j)
    			printf(" %3d", dmxTx[k][j]);
    		printf("\n");
    		dmxwrite(dmxHandle, sizeof(dmxTx[k]), dmxTx[k]);
    //		dmxrecv(dmxHandle, sizeof(dmxRx), dmxRx);
    
    		Sleep(inter_frame_delay);
    		NbFramesSent++;
    		k=(k+1)%(sizeof(dmxTx)/sizeof(dmxTx[0]));
    	}
    	dmxclose(dmxHandle);
    
    	return 0;
    }
    
    or download one of these: dmxlibw.c dmxlibl.c dmxlibsim.c
  2. Compile and run programs normally. Have fun!

Using MIDI with Dev-C++

You need to include and to link library. And use the Windows Midi Functions.

Compile and run notes:

  1. Edit programs that use MIDI functions, such as this one:
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>   /* required before including mmsystem.h */
    #include <mmsystem.h>  /* multimedia functions (such as MIDI) for Windows */
    
    int main(int argc, char** argv) {
    	int ckey;                 // storage for the current keyboard key being pressed
    	BOOL noteState[128] = {FALSE}; // keeping track of when the note is on or off
    	int key;                  // MIDI note key parameter value
    	const int velocity = 100; // MIDI note velocity parameter value
    	int error;                // monitor the status of returning functions
    	HMIDIOUT midiOutDevice;   // MIDI device interface for sending MIDI output
    	int midiOutPort;          // select which MIDI output port to open
    
    	MIDIINCAPS midiInCaps;
    	printf("MIDI_IN ports\n");
    	for (unsigned midiPort=0; midiPort<midiInGetNumDevs(); ++midiPort) {
    		midiInGetDevCaps(midiPort, &midiInCaps, sizeof(midiInCaps));
    		printf("\t%s\n", midiInCaps.szPname);
    	}
    
    	MIDIOUTCAPS midiOutCaps;
    	printf("MIDI_OUT ports\n");
    	for (unsigned midiPort=0; midiPort<midiOutGetNumDevs(); ++midiPort) {
    		midiOutGetDevCaps(midiPort, &midiOutCaps, sizeof(midiOutCaps));
    		printf("\t%s\n", midiOutCaps.szPname);
    	}
    
    	// variable which is both an integer and an array of characters:
    	union { unsigned long word; unsigned char data[4]; } midiMessage;
    	// midiMessage.data[0] = command byte of the MIDI message, for example: 0x90
    	// midiMessage.data[1] = first data byte of the MIDI message, for example: 60
    	// midiMessage.data[2] = second data byte of the MIDI message, for example 100
    	// midiMessage.data[3] = not used for any MIDI messages, so set to 0
    	midiMessage.data[0] = 0x90;  // MIDI note-on message (requires to data bytes)
    	midiMessage.data[1] = 60;    // MIDI note-on message: Key number (60 = middle C)
    	midiMessage.data[2] = 100;   // MIDI note-on message: Key velocity (100 = loud)
    	midiMessage.data[3] = 0;     // Unused parameter
    
    	// Assign the MIDI output port number (from input or default to 0)
    	midiOutPort = (argc < 2) ? 0 : atoi(argv[1]);
    	printf("MIDI output port set to %d.\n", midiOutPort);
    
    	// Open the MIDI output port
    	error = midiOutOpen(&midiOutDevice, midiOutPort, 0, 0, CALLBACK_NULL);
    	if (error != MMSYSERR_NOERROR) {
    		printf("Error opening MIDI Output.\n");
    		return 1;
    	}
    
    	// Main event loop
    	printf("Press \"esc\" to quit.\n");
    	do { // event loop
    		if (kbhit()) {   // If a key on the computer keyboard has been pressed 
    			ckey = getch();
    			key = 0;
    			switch (ckey) {
    			case 'a':
    				key = 36;
    				break;
    			case 's':
    				key = 38;
    				break;
    			case 'd':
    				key = 40;
    				break;
    			case 'f':
    				key = 41;
    				break;
    			case 'g':
    				key = 43;
    				break;
    			case 'h':
    				key = 45;
    				break;
    			case 'j':
    				key = 47;
    				break;
    			case 'k':
    				key = 48;
    				break;
    			default:
    				break;
    			}
    			if(key != 0) {
    				midiMessage.data[1] = key;
    				// Note is currently off, turn on note.
    				// Note is currently on, turn off note.
    				noteState[key] = !noteState[key];
    				midiMessage.data[2] = noteState[key] ? velocity : 0;
    				printf("Note turned %s.\n", noteState[key] ? "ON" : "OFF");
    			}
    
    			error = midiOutShortMsg(midiOutDevice, midiMessage.word);
    			if (error != MMSYSERR_NOERROR) {
    				printf("Warning: MIDI Output is not open.\n");
    			}
    		}
    	} while (ckey != '\e');
    
    	// turn any MIDI notes currently playing:
    	midiOutReset(midiOutDevice);
    
    	// Remove any data in MIDI device and close the MIDI Output port
    	midiOutClose(midiOutDevice);
    
    	return 0;
    }
    
    or download one of these:  devcpp-midi.c  devcpp-midiIn.c  devcpp-midiOut.c  devcpp-midiOutLoop.c  devcpp-midiOutLoopSMF.c  devcpp-midiOutSysex.c  devcpp-midiInChords.c  devcpp-midiKeysplit.c  devcpp-midiOutSimple.c  mididemo.html  mididemo.zip  devcpp-mididemo.zip  devcpp-playMidiFile.c  devcpp-playwav.c  devcpp-playmid.c  devcpp-playmp3.c  devcpp-drum.c
  2. Instruct the linker to link the following libraries. From Tools → Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
    -lwinmm
  3. Compile and run programs normally. Have fun!

Using sockets with Dev-C++

You need to include and to link library. And use the Windows Socket Functions.

Compile and run notes:

  1. Edit programs that use socket functions, such as this one:
    #include <stdio.h>
    #include <winsock2.h>
    #include <windows.h>
    int main() {
    	WSADATA wsaData;
    	if(WSAStartup(MAKEWORD(2, 1), &wsaData))
    	{
    		printf("\n::Error on WSAStartup()::\n");
    		return 1;
    	}
    
    	SOCKET mySocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(mySocket==INVALID_SOCKET)
    	{
    		printf("\n::Error on socket()::\n");
    		printf("Error Code: %d", WSAGetLastError());
    		WSACleanup();
    		return 1;
    	}
    
    	...
    
    	int intErr = closesocket(mysocket);
    	if(intErr==SOCKET_ERROR)
    	{
    		printf("\n::Error on closesocket()::\n");
    		printf("Error Code: %d", WSAGetLastError());
    		return 1;
    	}
    
    	WSACleanup();
    
    	return 0;
    }
    
    or download a couple of these:  devcpp-tcpServer.c  devcpp-tcpClient.c  devcpp-udpServer.c  devcpp-udpClient.c
  2. Instruct the linker to link the following libraries. From Tools → Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
    -lws2_32
  3. Compile and run programs normally. Have fun!

Using mailslot/namedpipes with Dev-C++

You need to include and to link library. And use the Windows Mailslot Functions or the Windows Pipes.

Compile and run notes:

  1. Edit programs that use socket functions, such as this one:
    
    or download a couple of these:
    	 devcpp-namedpipes-client.c
    	 devcpp-namedpipes-server.c
    	 devcpp-mailslot-reader.c
    	 devcpp-mailslot-writer.c
    
  2. Instruct the linker to link no additional libraries.
  3. Compile and run programs normally. Have fun!

Using Sound (play a single note) with Dev-C++

You need to include and to link library. And use the Windows Beep Function.

Compile and run notes:

  1. Edit programs that use Beep/Sleep functions, such as this one:
    #include <stdio.h>
    #include <windows.h>
    
    #define E3 440
    #define F3 480
    #define full 600
    #define half full/2
    
    int main() {
    	Beep(E3, full);
    	Sleep(half);
    	Beep(F3, full);
    
    	return 0;
    }
    
    or download one of these:  devcpp-sound.c
  2. Instruct the linker to link no additional libraries.
  3. Compile and run programs normally. Have fun!

Using FileVersionInfo (Windows only) with DevC++

You need to include and to link library. And use the Windows VersionInfo Functions.

Compile and run notes:

  1. Edit programs that use VersionInfo functions, such as this one:
    #include <stdio.h>
    #include <windows.h>
    
    int getVersionInfo(const char *filename, char *ver) {
    	DWORD dwHandle, size = GetFileVersionInfoSize(filename, &dwHandle);
    	if (0 == size)
    	{
    		return 1;
    	}
    	LPVOID *lpvi = malloc(size);
    	if ( !GetFileVersionInfo(filename, dwHandle, size, lpvi) )
    	{
    		free(lpvi);
    		return 2;
    	}
    
    	VS_FIXEDFILEINFO * pffi;
    	UINT sizeFFI = sizeof(VS_FIXEDFILEINFO);
    	if ( !VerQueryValue(lpvi, "\\", (LPVOID*)&pffi, &sizeFFI) )
    	{
    		free(lpvi);
    		return 3;
    	}
    
    	sprintf(ver, "Product: %d.%d.%d.%d\n"
    		, HIWORD(pffi->dwProductVersionMS)
    		, LOWORD(pffi->dwProductVersionMS)
    		, HIWORD(pffi->dwProductVersionLS)
    		, LOWORD(pffi->dwProductVersionLS)
    	);
    
    	printf("Product: %d.%d.%d.%d\n"
    		, HIWORD(pffi->dwProductVersionMS)
    		, LOWORD(pffi->dwProductVersionMS)
    		, HIWORD(pffi->dwProductVersionLS)
    		, LOWORD(pffi->dwProductVersionLS)
    	);
    
    	printf("File: %d.%d.%d.%d\n"
    		, HIWORD(pffi->dwFileVersionMS)
    		, LOWORD(pffi->dwFileVersionMS)
    		, HIWORD(pffi->dwFileVersionLS)
    		, LOWORD(pffi->dwFileVersionLS)
    	);
    
    	free(lpvi);
    	return 0;
    }
    int main() {
    	char szTemp[32+1];
    	getVersionInfo("c:\\windows\\notepad.exe", szTemp);
    	printf("%s\n", szTemp);
    	return 0;
    }
    
    or download one of these:  getfreemem.c
  2. Instruct the linker to link the following libraries. From Tools → Compiler Options, choose the General tab from the pop-up window and type the following into the Linker area:
    -lversion
  3. Compile and run programs normally. Have fun!