-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapyard
91 lines (74 loc) · 2.74 KB
/
scrapyard
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
89
90
91
if (file.length() < SECTOR_BYTES) {
/* we need to write the chunk offset table */
for (int i = 0; i < SECTOR_INTS; ++i) {
file.writeInt(0);
}
// write another sector for the timestamp info
for (int i = 0; i < SECTOR_INTS; ++i) {
file.writeInt(0);
}
sizeDelta += SECTOR_BYTES * 2;
}
if ((file.length() & 0xfff) != 0) {
/* the file size is not a multiple of 4KB, grow it */
for (int i = 0; i < (file.length() & 0xfff); ++i) {
file.write((byte) 0);
}
}
SDL_Surface* Drawer::renderWorld(RegionFileWorld& world)
{
//Find the lowest X and Z coordinates for proper offset
MC_Point offset = topleftOffset(world);
/* Then, create an image of the size of the world in blocks
* to place all of the region renders on */
MC_Point worldSize = world.getSize();
//The "Window" and the renderer
window = SDL_CreateWindow("", 0, 0, worldSize.x, worldSize.z, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if(!window || !renderer) {
error("Could not create window or renderer: ", SDL_GetError());
}
SDL_Texture* texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET ,worldSize.x, worldSize.z);
if(!texture) {
std::cerr << SDL_GetError() << std::endl;
exit(1);
}
//Set a texture to render to
SDL_SetRenderTarget( renderer, texture );
//Blank out the image
SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0x00 );
SDL_RenderClear(renderer);
for(auto& pair : world.getAllRegions())
{
/* Where to render the region. This is the region's position,
* plus the top-left offset */
int x = (pair.first.x + offset.x) * regionsize;
int z = (pair.first.z + offset.z) * regionsize;
renderRegion({x,z}, pair.second);
}
//Unlink render target
SDL_SetRenderTarget( renderer, nullptr );
//Copy texture (that we drew on) to the screen
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
//Show the drawing
SDL_RenderPresent( renderer );
//Get pixel data from texture to return
SDL_Surface* surface = createRGBASurface(worldSize.x, worldSize.z);
if(!surface) {
error("Cannot create render surface: ", SDL_GetError());
}
//Copy pixels from texture into surface
if( SDL_RenderReadPixels(renderer, nullptr,
surface->format->format,
surface->pixels,
surface->pitch) != 0 )
{
std::cout << SDL_GetError() << std::endl;
}
//Add cool grid lines
if(config::get().GetInt("drawer-draw-gridlines")) {
drawGirdLines(surface);
}
return surface;
}