You need to include and to link library. And use the Windows GDI Functions.
#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
-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
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.
-lsfml-audio
-lsfml-window
-lsfml-graphics
#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;
}
You need to include and to link library. And use the Windows Console Functions
#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;
}
-laltconio
#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;
}
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
#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
You need to include and to link library. And use the Windows Midi Functions.
#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
-lwinmm
You need to include and to link library. And use the Windows Socket Functions.
#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
-lws2_32
You need to include and to link library. And use the Windows Mailslot Functions or the Windows Pipes.
or download a couple of these: devcpp-namedpipes-client.c devcpp-namedpipes-server.c devcpp-mailslot-reader.c devcpp-mailslot-writer.c
You need to include and to link library. And use the Windows Beep Function.
#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
You need to include and to link library. And use the Windows VersionInfo Functions.
#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
-lversion