-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (72 loc) · 2.62 KB
/
main.cpp
File metadata and controls
88 lines (72 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <fstream>
#include "tgfx/core/Surface.h"
#include "tgfx/core/Canvas.h"
#include "tgfx/core/Paint.h"
#include "tgfx/core/Path.h"
#include "tgfx/core/Rect.h"
#include "tgfx/core/Color.h"
#include "tgfx/core/ImageCodec.h"
#include "tgfx/core/Pixmap.h"
#include "tgfx/gpu/opengl/GLDevice.h"
int main() {
auto device = tgfx::GLDevice::Make();
if (!device) {
std::cerr << "Failed to create GLDevice!" << std::endl;
return -1;
}
auto context = device->lockContext();
if (!context) {
std::cerr << "Failed to lock context!" << std::endl;
return -1;
}
const int canvasWidth = 800;
const int canvasHeight = 600;
auto surface = tgfx::Surface::Make(context, canvasWidth, canvasHeight);
if (!surface) {
std::cerr << "Failed to create surface!" << std::endl;
device->unlock();
return -1;
}
auto canvas = surface->getCanvas();
canvas->clear(tgfx::Color::White());
tgfx::Paint paint;
paint.setColor(tgfx::Color::Red());
tgfx::Rect redRect = tgfx::Rect::MakeXYWH(50, 50, 200, 150);
canvas->drawRect(redRect, paint);
paint.setColor(tgfx::Color::Blue());
tgfx::Rect blueOval = tgfx::Rect::MakeXYWH(300, 100, 150, 150);
canvas->drawOval(blueOval, paint);
paint.setColor(tgfx::Color::Green());
tgfx::Path greenTriangle;
greenTriangle.moveTo(500, 200);
greenTriangle.lineTo(600, 100);
greenTriangle.lineTo(700, 200);
greenTriangle.close();
canvas->drawPath(greenTriangle, paint);
auto imageInfo = tgfx::ImageInfo::Make(canvasWidth, canvasHeight, tgfx::ColorType::RGBA_8888, tgfx::AlphaType::Premultiplied);
auto pixelBuffer = std::make_unique<uint8_t[]>(imageInfo.byteSize());
if (!surface->readPixels(imageInfo, pixelBuffer.get())) {
std::cerr << "Failed to read pixels!" << std::endl;
device->unlock();
return -1;
}
tgfx::Pixmap pixmap(imageInfo, pixelBuffer.get());
auto encodedData = tgfx::ImageCodec::Encode(pixmap, tgfx::EncodedFormat::WEBP, 100);
if (!encodedData) {
std::cerr << "Failed to encode image!" << std::endl;
device->unlock();
return -1;
}
std::ofstream outputFile("demo_output.webp", std::ios::binary);
if (!outputFile) {
std::cerr << "Failed to create output file!" << std::endl;
device->unlock();
return -1;
}
outputFile.write(reinterpret_cast<const char*>(encodedData->data()), static_cast<std::streamsize>(encodedData->size()));
outputFile.close();
std::cout << "Image saved as demo_output.webp" << std::endl;
device->unlock();
return 0;
}