[Linux – Ubuntu – C/C++] Lập trình C với đồ họa trên Linux (Ubuntu) – Programming C with Graphics on Linux (Ubuntu)
Mở màn các bạn có thể xem hình minh họa sau:

Để thực hiện được điều này trước tiên các bạn kiểm tra lại các gói cơ bản đã cài chưa:
sudo apt-get install build-essential
Tiếp theo hãy chạy lệnh sau trong Terminal để cài các gói cần thiết (Có thể trước khi chạy lệnh này các bạn cần update):
sudo apt-get update
sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev
Bây giờ hãy download libgraph tại đây
Copy the file libgraph-1.0.2.tar.gz vào home folder. Click chuột phải và chọn Extract here.
Chạy tiếp các lệnh sau:
cd libgraph-1.0.2
./configure
sudo make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib
Bình thường với windows chúng ta có câu lệnh khởi tạo chế độ đồ họa:
int gd=DETECT,gm; initgraph(&gd,&gm,"c:tcbgi");
Và bây giờ ta làm tuơng tự có điều thay “c:tcbgi” bằng NULL
int gd=DETECT,gm; initgraph(&gd,&gm,NULL);
Cuối cùng tạo 1 chuơng trình và biên dịch file graphics.cpp với lệnh sau:
g++ graphics.cpp -o graphics.o -lgraph
Chạy file graphics.o
./graphics.o
Một ví dụ đơn giản:
#include<graphics.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
int gd,gm=VGAMAX; gd=DETECT;
initgraph(&gd,&gm,NULL);
setbkcolor(1);
cleardevice();
setcolor(4);
char s1[] = "nguyenvanquan7826",
s2[] = "Welcome to C++ graphics!",
s3[] = "graphics on Linux";
outtextxy(100,60,s3);
outtextxy(70,40,s2);
outtextxy(130,80,s1);
setcolor(14);
line(50,200,400,200);
setcolor(4);
for (int i=1; i<20; i++)
{
setcolor(i);
delay(500);
circle(100+10*i,200,80);
}
cin.get();
return 0;
}
Kết quả:

update thêm 1 ảnh minh họa:

#include<graphics.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
setbkcolor(1); // set backgroud
int x = 250;
setcolor(12); //outline heart
circle(x+50,50,40);
delay(1000);
circle(x+110,50,40);
delay(500);
line(x+22,80,x+80,140);
delay(500);
line(x+80,140,x+138,80);
delay(500);
floodfill(x+50,50,12); //fill heart
delay(500);
floodfill(x+110,50,12);
delay(500);
floodfill(x+80,50,12);
delay(500);
floodfill(x+80,100,12);
delay(500);
setcolor(RED);
outtextxy(x+35,50,"We love Linux");
outtextxy(x+35,150,"VietSource.net");
cin.get();
closegraph();
return 0;
}
Tham khảo:
Lập trình C trên Ubuntu (Linux)
Tài liệu đồ họa trong C: dowload
Tuy nhiên hiện tại vẫn chưa biết cách đặt màu cho chữ. Nếu bạn biết cách khắc phục rất mong bạn chia sẻ.
Bài viết có tham khảo tại: http://blog.eternal-thinker.com



ban oi the đối với bài toán 8 con hậu thì tn
Bài 8 con hậu liên quan gì đến cái này đâu bạn?
Thuật toán của nó bạn search GG nhé. 🙂
Vất luôn cai libgraph đi bạn. No la bộ lib viết trên SDL nên nếu thich đồ họa thi dung luôn sdl.
cho mình hỏi lúc nhập ./graphics.o thì vẫn chạy nhưng ma nó báo lỗi:
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
1: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost’ failed.
Thế làm thế nào để fix a?
Using nameplace để làm gì vậy a
Để có thể dùng được các lệnh trong std. VD lệnh cin.