Skip to content

Commit b3e0dcf

Browse files
authored
Merge pull request #125 from lrusak/modernization
Modernization
2 parents bfa29de + 79ebdae commit b3e0dcf

16 files changed

Lines changed: 133 additions & 201 deletions

File tree

src/colorfire/main.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <gli/gli.hpp>
2525
#include <bzlib.h>
2626

27-
#define LOAD_TEXTURE(dest, src, compressedSize, size) dest = (unsigned char *)malloc(size); BZ2_bzBuffToBuffDecompress((char *)dest, &size, (char *)src, compressedSize, 0, 0);
28-
#define FREE_TEXTURE(tex) free(tex);
29-
3027
// Override GL_RED if not present with GL_LUMINANCE, e.g. on Android GLES
3128
#ifndef GL_RED
3229
#define GL_RED GL_LUMINANCE
@@ -72,20 +69,20 @@ bool CScreensaverColorFire::Start()
7269
{
7370
case TYPE_SMOKE:
7471
{
75-
LOAD_TEXTURE(l_tex, smokemap, smokemap_compressedsize, smokemap_size);
7672
gli::texture Texture(gli::TARGET_2D, gli::FORMAT_RGB8_UNORM_PACK8, gli::texture::extent_type(TEXSIZE, TEXSIZE, 1), 1, 1, 1);
77-
std::memcpy(Texture.data(), l_tex, Texture.size());
73+
74+
BZ2_bzBuffToBuffDecompress(reinterpret_cast<char*>(Texture.data()), &smokemap_size, const_cast<char*>(smokemap), smokemap_compressedsize, 0, 0);
75+
7876
m_texture = kodi::gui::gl::Load(Texture);
79-
FREE_TEXTURE(l_tex)
8077
break;
8178
}
8279
case TYPE_RIPPLES:
8380
{
84-
LOAD_TEXTURE(l_tex, ripplemap, ripplemap_compressedsize, ripplemap_size);
8581
gli::texture Texture(gli::TARGET_2D, gli::FORMAT_RGB8_UNORM_PACK8, gli::texture::extent_type(TEXSIZE, TEXSIZE, 1), 1, 1, 1);
86-
std::memcpy(Texture.data(), l_tex, Texture.size());
82+
83+
BZ2_bzBuffToBuffDecompress(reinterpret_cast<char*>(Texture.data()), &ripplemap_size, const_cast<char*>(ripplemap), ripplemap_compressedsize, 0, 0);
84+
8785
m_texture = kodi::gui::gl::Load(Texture);
88-
FREE_TEXTURE(l_tex)
8986
break;
9087
}
9188
case TYPE_SMOOTH:

src/cyclone/main.cpp

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "main.h"
1818

19+
#include <array>
20+
#include <vector>
1921
#include <chrono>
2022
#include <algorithm>
2123
#include <glm/glm.hpp>
@@ -96,9 +98,9 @@ int factorial(int x)
9698
class CCyclone
9799
{
98100
public:
99-
float **m_targetxyz;
100-
float **m_xyz;
101-
float **m_oldxyz;
101+
std::vector<std::array<float, 3>> m_targetxyz;
102+
std::vector<std::array<float, 3>> m_xyz;
103+
std::vector<std::array<float, 3>> m_oldxyz;
102104
float *m_targetWidth;
103105
float *m_width;
104106
float *m_oldWidth;
@@ -110,29 +112,24 @@ class CCyclone
110112
float m_hslChange[2];
111113

112114
CCyclone();
113-
~CCyclone();
115+
~CCyclone() = default;
114116
void Update(CScreensaverCyclone* base);
115117

116118
private:
117-
sLight* m_curves = nullptr;
119+
std::vector<sLight> m_curves;
118120
};
119121

120122
CCyclone::CCyclone()
121123
{
122124
int i;
123125

124126
// Initialize position stuff
125-
m_curves = new sLight[std::max(gCycloneSettings.dComplexity + 3, 50)];
127+
m_curves.resize(std::max(gCycloneSettings.dComplexity + 3, 50));
128+
129+
m_targetxyz.resize(gCycloneSettings.dComplexity+3);
130+
m_xyz.resize(gCycloneSettings.dComplexity+3);
131+
m_oldxyz.resize(gCycloneSettings.dComplexity+3);
126132

127-
m_targetxyz = new float*[gCycloneSettings.dComplexity+3];
128-
m_xyz = new float*[gCycloneSettings.dComplexity+3];
129-
m_oldxyz = new float*[gCycloneSettings.dComplexity+3];
130-
for (i = 0; i < int(gCycloneSettings.dComplexity)+3; i++)
131-
{
132-
m_targetxyz[i] = new float[3];
133-
m_xyz[i] = new float[3];
134-
m_oldxyz[i] = new float[3];
135-
}
136133
m_xyz[gCycloneSettings.dComplexity+2][0] = rsRandf(float(WIDTH*2)) - float(WIDTH);
137134
m_xyz[gCycloneSettings.dComplexity+2][1] = float(HIGHT);
138135
m_xyz[gCycloneSettings.dComplexity+2][2] = rsRandf(float(WIDTH*2)) - float(WIDTH);
@@ -184,22 +181,6 @@ CCyclone::CCyclone()
184181
m_hslChange[1] = 10.0f;
185182
}
186183

187-
CCyclone::~CCyclone()
188-
{
189-
for (int i = 0; i < int(gCycloneSettings.dComplexity) + 3; i++)
190-
{
191-
delete[] m_targetxyz[i];
192-
delete[] m_xyz[i];
193-
delete[] m_oldxyz[i];
194-
}
195-
196-
delete[] m_targetxyz;
197-
delete[] m_xyz;
198-
delete[] m_oldxyz;
199-
200-
delete[] m_curves;
201-
}
202-
203184
void CCyclone::Update(CScreensaverCyclone* base)
204185
{
205186
int i;
@@ -378,15 +359,15 @@ void CCyclone::Update(CScreensaverCyclone* base)
378359
m_curves[ptr ].color = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
379360
m_curves[ptr++].vertex = point;
380361
}
381-
base->DrawEntry(GL_LINE_STRIP, m_curves, ptr);
362+
base->DrawEntry(GL_LINE_STRIP, m_curves.data(), ptr);
382363
ptr = 0;
383364

384365
for (i = 0; i < (gCycloneSettings.dComplexity+3); i++)
385366
{
386367
m_curves[ptr ].color = glm::vec4(1.0f, 0.0f, 0.0f, 1.0f);
387368
m_curves[ptr++].vertex = glm::vec3(m_xyz[i][0], m_xyz[i][1], m_xyz[i][2]);
388369
}
389-
base->DrawEntry(GL_LINE_STRIP, m_curves, ptr);
370+
base->DrawEntry(GL_LINE_STRIP, m_curves.data(), ptr);
390371
base->m_lightingEnabled = 1;
391372
}
392373
}
@@ -535,13 +516,22 @@ bool CScreensaverCyclone::Start()
535516
// Initialize cyclones and their particles
536517
for (i = 0; i < 13; i++)
537518
m_fact[i] = float(factorial(i));
538-
m_cyclones = new CCyclone*[gCycloneSettings.dCyclones];
539-
m_particles = new CParticle*[gCycloneSettings.dParticles * gCycloneSettings.dCyclones];
540-
for (i = 0; i < gCycloneSettings.dCyclones; i++)
519+
520+
m_cyclones.resize(gCycloneSettings.dCyclones);
521+
m_particles.resize(gCycloneSettings.dParticles * gCycloneSettings.dCyclones);
522+
523+
auto particles = m_particles.begin();
524+
525+
for (auto& cyclone : m_cyclones)
541526
{
542-
m_cyclones[i] = new CCyclone;
543-
for (j=i*gCycloneSettings.dParticles; j<((i+1)*gCycloneSettings.dParticles); j++)
544-
m_particles[j] = new CParticle(m_cyclones[i]);
527+
cyclone = std::make_unique<CCyclone>();
528+
529+
std::for_each(particles, particles + gCycloneSettings.dParticles, [&cyclone](auto& particle)
530+
{
531+
particle = std::make_unique<CParticle>(cyclone.get());
532+
});
533+
534+
particles += gCycloneSettings.dParticles;
545535
}
546536

547537
glGenBuffers(1, &m_vertexVBO);
@@ -565,10 +555,6 @@ void CScreensaverCyclone::Stop()
565555

566556
glDisable(GL_DEPTH_TEST);
567557
glDisable(GL_CULL_FACE);
568-
569-
// Free memory
570-
delete[] m_particles;
571-
delete[] m_cyclones;
572558
}
573559

574560
void CScreensaverCyclone::Render()

src/cyclone/main.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <kodi/gui/gl/Shader.h>
2222
#include <glm/gtc/type_ptr.hpp>
2323

24+
#include <memory>
25+
#include <vector>
26+
2427
struct sLight
2528
{
2629
glm::vec3 vertex;
@@ -93,8 +96,8 @@ class ATTR_DLL_LOCAL CScreensaverCyclone
9396

9497
GLuint m_vertexVBO = 0;
9598

96-
CCyclone **m_cyclones;
97-
CParticle **m_particles;
99+
std::vector<std::unique_ptr<CCyclone>> m_cyclones;
100+
std::vector<std::unique_ptr<CParticle>> m_particles;
98101

99102
float m_frameTime = 0.0f;
100103
bool m_startOK = false;

src/drempels/TexMgr.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ TexMgr::TexMgr()
3939

4040
TexMgr::~TexMgr()
4141
{
42-
delete m_imageThread;
4342
delete [] m_curTex;
4443
delete [] m_nextTex;
4544
}
@@ -51,7 +50,7 @@ void TexMgr::setImageDir(const std::string& newDirName)
5150

5251
void TexMgr::start()
5352
{
54-
m_imageThread = new std::thread(&TexMgr::imageThreadMain, this);
53+
m_imageThread = std::thread(&TexMgr::imageThreadMain, this);
5554
}
5655

5756
void TexMgr::stop()
@@ -63,7 +62,7 @@ void TexMgr::stop()
6362
m_nextTexCond.notify_one();
6463
}
6564

66-
m_imageThread->join();
65+
m_imageThread.join();
6766
}
6867

6968
bool TexMgr::getNext()

src/drempels/TexMgr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class TexMgr
5252
std::string m_dirName;
5353
DIR* m_imageDir = nullptr;
5454

55-
std::thread* m_imageThread = nullptr;
55+
std::thread m_imageThread;
5656
std::mutex m_nextTexMutex;
5757
std::condition_variable m_nextTexCond;
5858
volatile bool m_exiting = false;

src/drempels/main.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bool CScreensaverDrempels::Start()
130130
gSettings.dGenTexSize = 256;
131131
m_textureManager.setTexSize(256, 256);
132132

133-
m_fadeBuf = new uint32_t[256 * 256];
133+
m_fadeBuf.resize(256 * 256);
134134

135135
m_textureManager.setGenTexSize(gSettings.dGenTexSize, gSettings.dGenTexSize);
136136

@@ -211,10 +211,6 @@ void CScreensaverDrempels::Stop()
211211
m_startOK = false;
212212
m_textureManager.stop();
213213

214-
delete [] m_fadeBuf;
215-
delete [] m_cell;
216-
delete [] m_buf;
217-
218214
glBindBuffer(GL_ARRAY_BUFFER, 0);
219215
glDeleteBuffers(1, &m_vertexVBO);
220216
m_vertexVBO = 0;
@@ -311,7 +307,7 @@ void CScreensaverDrempels::Render()
311307
glDisable (GL_BLEND);
312308

313309
glBindTexture(GL_TEXTURE_2D, m_tex);
314-
glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, m_fadeBuf);
310+
glReadPixels(0, 0, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, m_fadeBuf.data());
315311
}
316312
else if (!m_fadeComplete)
317313
{
@@ -331,13 +327,11 @@ void CScreensaverDrempels::Render()
331327
const float scale = 0.45f + 0.1f*sinf(intframe2*0.01f);
332328
const float rot = m_animTime*gSettings.rotational_speed*6.28f;
333329

334-
if (m_cell == NULL)
335-
m_cell = new td_cellcornerinfo[UVCELLSX * UVCELLSY];
330+
m_cell.clear();
331+
m_cell.resize(UVCELLSX * UVCELLSY);
336332

337333
#define CELL(i,j) m_cell[((i) * UVCELLSX) + (j)]
338334

339-
memset(m_cell, 0, sizeof(td_cellcornerinfo)*(UVCELLSX)*(UVCELLSY));
340-
341335
#define NUM_MODES 7
342336

343337
float t[NUM_MODES];
@@ -573,8 +567,9 @@ void CScreensaverDrempels::Render()
573567
CELL(i,j).dsdy = (CELL(i,j+1).s - CELL(i,j).s) / (v_delta*FXH);
574568
}
575569

576-
if (m_buf == nullptr)
577-
m_buf = new unsigned short [FXW * FXH * 2];
570+
m_buf.clear();
571+
m_buf.resize(FXW * FXH * 2);
572+
578573
for (unsigned int jj = 0; jj < UVCELLSY - 2; jj += 2)
579574
{
580575
for (unsigned int ii = 0; ii < UVCELLSX - 2; ii += 2)
@@ -601,9 +596,9 @@ void CScreensaverDrempels::Render()
601596
glEnable(GL_BLEND);
602597
glBindTexture(GL_TEXTURE_2D, m_tex);
603598

604-
unsigned short *uvbuf = m_buf;
605-
uint32_t *texbuf = m_fadeComplete ? m_textureManager.getCurTex() : m_fadeBuf;
606-
uint32_t *outbuf = (uint32_t *)m_buf;
599+
unsigned short *uvbuf = m_buf.data();
600+
uint32_t *texbuf = m_fadeComplete ? m_textureManager.getCurTex() : m_fadeBuf.data();
601+
uint32_t *outbuf = (uint32_t *)m_buf.data();
607602
for (unsigned int ii = 0; ii < FXW * FXH; ++ii)
608603
{
609604
const uint16_t u0 = *uvbuf++;
@@ -622,7 +617,7 @@ void CScreensaverDrempels::Render()
622617
*outbuf++ = rgbLerp(l, r, u0);
623618
}
624619

625-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FXW, FXH, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_buf);
620+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FXW, FXH, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_buf.data());
626621

627622
DrawQuads(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f - blurAmount));
628623

src/drempels/main.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ATTR_DLL_LOCAL CScreensaverDrempels
5656
int m_cellResolution;
5757

5858
bool m_fadeComplete = false;
59-
uint32_t *m_fadeBuf = nullptr;
59+
std::vector<uint32_t> m_fadeBuf;
6060

6161
float m_animTime = 0;
6262

@@ -71,8 +71,8 @@ class ATTR_DLL_LOCAL CScreensaverDrempels
7171

7272
double m_lastTexChange = 0;
7373

74-
td_cellcornerinfo *m_cell = nullptr;
75-
unsigned short *m_buf = nullptr;
74+
std::vector<td_cellcornerinfo> m_cell;
75+
std::vector<unsigned short> m_buf;
7676

7777
glm::mat4 m_projMat;
7878
glm::mat4 m_modelMat;

0 commit comments

Comments
 (0)