Skip to content

Commit be6b813

Browse files
Adding examples
1 parent 9be5ecb commit be6b813

File tree

5 files changed

+291
-51
lines changed

5 files changed

+291
-51
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* runcpp2
2+
3+
Dependencies:
4+
- Name: civetweb
5+
Platforms: [DefaultPlatform]
6+
Source:
7+
Git:
8+
URL: "https://github.com/civetweb/civetweb.git"
9+
LibraryType: Static
10+
IncludePaths:
11+
- "./include"
12+
LinkProperties:
13+
Windows:
14+
DefaultProfile:
15+
SearchLibraryNames: ["civetweb"]
16+
SearchDirectories: ["./output/src/debug"]
17+
Unix:
18+
DefaultProfile:
19+
SearchLibraryNames: ["civetweb"]
20+
SearchDirectories: ["./output/src"]
21+
Setup: ["mkdir output", "cd output && cmake .."]
22+
Build: ["cd output && cmake --build . -j 16"]
23+
*/
24+
25+
extern "C" {
26+
#include "civetweb.h"
27+
}
28+
29+
#include <cstring>
30+
#include <stdlib.h>
31+
#include <chrono>
32+
#include <thread>
33+
34+
volatile int exitNow = 0;
35+
36+
int ExitHandler(struct mg_connection *conn, void *cbdata)
37+
{
38+
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n");
39+
mg_printf(conn, "Server will shut down.\n");
40+
mg_printf(conn, "Bye!\n");
41+
exitNow = 1;
42+
return 1;
43+
}
44+
45+
int main()
46+
{
47+
const char *options[] =
48+
{
49+
"document_root",
50+
".",
51+
"listening_ports",
52+
"8080",
53+
"request_timeout_ms",
54+
"10000",
55+
"error_log_file",
56+
"error.log",
57+
"enable_auth_domain_check",
58+
"no",
59+
0
60+
};
61+
62+
mg_callbacks callbacks = {0};
63+
mg_context* ctx = nullptr;
64+
mg_server_port ports[32];
65+
int port_cnt, n;
66+
int err = 0;
67+
68+
ctx = mg_start(&callbacks, 0, options);
69+
70+
/* Check return value: */
71+
if (ctx == NULL)
72+
{
73+
fprintf(stderr, "Cannot start CivetWeb - mg_start failed.\n");
74+
return EXIT_FAILURE;
75+
}
76+
77+
mg_set_request_handler(ctx, "/exit", ExitHandler, 0);
78+
79+
/* List all listening ports */
80+
memset(ports, 0, sizeof(ports));
81+
port_cnt = mg_get_server_ports(ctx, 32, ports);
82+
83+
printf("\n%i listening ports:\n\n", port_cnt);
84+
for(n = 0; n < port_cnt && n < 32; n++)
85+
{
86+
const char *proto = ports[n].is_ssl ? "https" : "http";
87+
const char *host;
88+
89+
if((ports[n].protocol & 1) == 1)
90+
{
91+
host = "127.0.0.1";
92+
printf("Browse files at %s://%s:%i/\n", proto, host, ports[n].port);
93+
printf("Exit at %s://%s:%i%s\n", proto, host, ports[n].port, "/exit");
94+
printf("\n");
95+
}
96+
}
97+
98+
while(!exitNow)
99+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
100+
101+
mg_stop(ctx);
102+
return 0;
103+
}
104+

Examples/CppStaticWebServer.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* runcpp2
2+
3+
Dependencies:
4+
- Name: cpp-httplib
5+
Platforms: [DefaultPlatform]
6+
Source:
7+
Git:
8+
URL: "https://github.com/yhirose/cpp-httplib.git"
9+
Branch: "v0.20.0"
10+
LibraryType: Header
11+
IncludePaths:
12+
- "./"
13+
*/
14+
15+
#include "httplib.h"
16+
#include <iostream>
17+
18+
int main()
19+
{
20+
httplib::Server svr;
21+
if(!svr.set_mount_point("/", "./"))
22+
return 1;
23+
24+
svr.set_file_extension_and_mimetype_mapping("cpp", "text/x-c");
25+
std::cout << "Running on localhost:8080" << std::endl;
26+
svr.listen("0.0.0.0", 8080);
27+
28+
return 0;
29+
}
30+

Examples/MatplotExample.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* runcpp2
2+
3+
Dependencies:
4+
- Name: matplotplusplus
5+
Platforms: [DefaultPlatform]
6+
Source:
7+
Git:
8+
URL: "https://github.com/alandefreitas/matplotplusplus.git"
9+
Branch: "v1.2.2"
10+
LibraryType: Shared
11+
IncludePaths: ["./source", "./build/source/matplot"]
12+
LinkProperties:
13+
Unix:
14+
DefaultProfile:
15+
SearchLibraryNames: ["libmatplot.so.1"]
16+
ExcludeLibraryNames: ["libmatplot.so.1.2.0"]
17+
SearchDirectories: ["./build/source/matplot"]
18+
Windows:
19+
DefaultProfile:
20+
SearchLibraryNames: ["libmatplot"]
21+
SearchDirectories: ["./build/source/matplot/debug"]
22+
Setup:
23+
- "mkdir build"
24+
- "cd build && cmake .. -DMATPLOTPP_BUILD_WITH_SANITIZERS=OFF -DMATPLOTPP_BUILD_EXAMPLES=OFF -DMATPLOTPP_BUILD_INSTALLER=OFF -DMATPLOTPP_BUILD_PACKAGE=OFF -DBUILD_SHARED_LIBS=ON"
25+
- "cd build && cmake --build . -j 16"*/
26+
27+
#include "matplot/matplot.h"
28+
29+
#include <cstdlib>
30+
#include <iostream>
31+
32+
int main()
33+
{
34+
//NOTE: Alternatively, you can put this under "Setup" section in runcpp2 build settings
35+
#ifdef _WIN32
36+
if(system("which gnuplot") != 0)
37+
#else
38+
if(system("where gnuplot") != 0)
39+
#endif
40+
{
41+
std::cout << "Failed to find gnuplot, please install it on the system" << std::endl;
42+
return 1;
43+
}
44+
45+
auto f1 = matplot::figure();
46+
matplot::plot(matplot::vector_1d{1., 2., 3.}, matplot::vector_1d{2., 4., 6.});
47+
48+
49+
auto f2 = matplot::figure();
50+
matplot::scatter(matplot::iota(1, 20), matplot::rand(20, 0, 1));
51+
52+
matplot::show();
53+
return 0;
54+
}

Examples/SDLWindow.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/* runcpp2
2+
3+
Dependencies:
4+
- Name: SDL2
5+
Platforms: [DefaultPlatform]
6+
Source:
7+
Git:
8+
URL: "https://github.com/libsdl-org/SDL.git"
9+
Branch: "release-2.32.2"
10+
11+
LibraryType: Shared
12+
IncludePaths:
13+
- "./include"
14+
LinkProperties:
15+
Windows:
16+
DefaultProfile:
17+
SearchLibraryNames: ["SDL2"]
18+
SearchDirectories: ["./build/debug"]
19+
Unix:
20+
DefaultProfile:
21+
SearchLibraryNames: ["SDL2"]
22+
SearchDirectories: ["./build"]
23+
Setup: ["mkdir build", "cd build && cmake .."]
24+
Build: ["cd build && cmake --build . -j 16"]
25+
26+
# Or use system installed SDL on Unix...
27+
# OverrideLinkFlags:
28+
# Unix:
29+
# "g++":
30+
# Remove: ""
31+
# Append: "-lSDL2"
32+
33+
*/
34+
35+
36+
#include <SDL2/SDL.h>
37+
38+
#include <iostream>
39+
40+
const int SCREEN_WIDTH = 800;
41+
const int SCREEN_HEIGHT = 600;
42+
43+
//Credits: https://github.com/AlmasB/SDL2-Demo/blob/master/src/Main.cpp
44+
// https://github.com/aminosbh/basic-cpp-sdl-project/blob/master/src/main.cc
45+
int main(int arc, char ** argv)
46+
{
47+
48+
SDL_Window* window = nullptr;
49+
50+
if (SDL_Init( SDL_INIT_VIDEO ) < 0)
51+
std::cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
52+
else
53+
{
54+
window = SDL_CreateWindow( "SDL2 Demo",
55+
SDL_WINDOWPOS_CENTERED,
56+
SDL_WINDOWPOS_CENTERED,
57+
SCREEN_WIDTH,
58+
SCREEN_HEIGHT,
59+
SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
60+
}
61+
62+
if(!window)
63+
return 1;
64+
65+
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
66+
if(!renderer)
67+
return 1;
68+
69+
SDL_Rect squareRect;
70+
71+
//Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT)
72+
squareRect.w = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
73+
squareRect.h = std::min(SCREEN_WIDTH, SCREEN_HEIGHT) / 2;
74+
75+
//Square position: In the middle of the screen
76+
squareRect.x = SCREEN_WIDTH / 2 - squareRect.w / 2;
77+
squareRect.y = SCREEN_HEIGHT / 2 - squareRect.h / 2;
78+
79+
bool quit = false;
80+
SDL_Event e;
81+
82+
while (!quit)
83+
{
84+
while (SDL_PollEvent(&e) != 0)
85+
{
86+
if (e.type == SDL_QUIT)
87+
quit = true;
88+
89+
// Initialize renderer color white for the background
90+
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
91+
SDL_RenderClear(renderer);
92+
// Set renderer color red to draw the square
93+
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
94+
SDL_RenderFillRect(renderer, &squareRect);
95+
SDL_RenderPresent(renderer);
96+
}
97+
}
98+
99+
SDL_DestroyRenderer(renderer);
100+
SDL_DestroyWindow(window);
101+
SDL_Quit();
102+
return 0;
103+
}

Examples/testSDL.cpp

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)