Fix a bunch of compiler warnings (backported from 'master')

Many warnings were benign, some were false positives, but better
fix a warning than miss an error.

test/fractals.cxx: backported from 1.4
This commit is contained in:
Albrecht Schlosser 2024-03-15 21:26:51 +01:00
parent 25d7ed7e0d
commit 4f787cc1f0
3 changed files with 112 additions and 113 deletions

View File

@ -88,7 +88,7 @@ private:
return(_size);
}
void size(int count) {
if ( count != _size ) {
if ( count > 0 && count != _size ) {
arr = (char*)realloc(arr, count * sizeof(char));
_size = count;
}

View File

@ -95,7 +95,8 @@ static int scroll_x = 0;
*/
Fl_Text_Display::Fl_Text_Display(int X, int Y, int W, int H, const char* l)
: Fl_Group(X, Y, W, H, l) {
int i;
#define VISIBLE_LINES_INIT 1 // allow compiler to remove unused code (PR #582)
mMaxsize = 0;
damage_range1_start = damage_range1_end = -1;
@ -148,11 +149,14 @@ Fl_Text_Display::Fl_Text_Display(int X, int Y, int W, int H, const char* l)
mStyleBuffer = 0;
mStyleTable = 0;
mNStyles = 0;
mNVisibleLines = 1;
mNVisibleLines = VISIBLE_LINES_INIT;
mLineStarts = new int[mNVisibleLines];
mLineStarts[0] = 0;
for (i=1; i<mNVisibleLines; i++)
mLineStarts[i] = -1;
#if VISIBLE_LINES_INIT > 1
{ // Note: this code is unused unless mNVisibleLines is ever initialized > 1
for (int i=1; i<mNVisibleLines; i++) mLineStarts[i] = -1;
}
#endif
mSuppressResync = 0;
mNLinesDeleted = 0;
mModifyingTabDistance = 0;

View File

@ -1,23 +1,21 @@
//
// "$Id$"
//
// Fractal drawing demo for the Fast Light Tool Kit (FLTK).
//
// This is a GLUT demo program, with modifications to
// demonstrate how to add FLTK controls to a GLUT program. The GLUT
// code is unchanged except for the end (search for FLTK to find changes).
//
// Copyright 1998-2016 by Bill Spitzak and others.
// Copyright 1998-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
// https://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
// Please see the following page on how to report bugs and issues:
//
// http://www.fltk.org/str.php
// https://www.fltk.org/bugs.php
//
#include <config.h>
@ -34,10 +32,10 @@ int main(int, char**) {
*
* Usage: fractals
*
* Homework 6, Part 2: fractal mountains and fractal trees
* Homework 6, Part 2: fractal mountains and fractal trees
* (Pretty Late)
*
* Draws fractal mountains and trees -- and an island of mountains in water
* Draws fractal mountains and trees -- and an island of mountains in water
* (I tried having trees on the island but it didn't work too well.)
*
* Two viewer modes: polar and flying (both restrained to y>0 for up vector).
@ -75,17 +73,18 @@ int main(int, char**) {
# define srand48(x) (srand((x)))
#endif
typedef enum { NOTALLOWED, MOUNTAIN, TREE, ISLAND, BIGMTN, STEM, LEAF,
typedef enum { NOTALLOWED, MOUNTAIN, TREE, ISLAND, BIGMTN, STEM, LEAF,
MOUNTAIN_MAT, WATER_MAT, LEAF_MAT, TREE_MAT, STEMANDLEAVES,
AXES } DisplayLists;
// Note: MAXLEVEL is the highest level, range is 0..MAXLEVEL
#define MAXLEVEL 8
int Rebuild = 1, /* Rebuild display list in next display? */
fractal = TREE, /* What fractal are we building */
Level = 4; /* levels of recursion for fractals */
Level = 4; /* levels of recursion for fractals */
int DrawAxes = 0;
int DrawAxes = 0;
/***************************************************************/
/************************* VECTOR JUNK *************************/
@ -97,7 +96,7 @@ void printvert(float v[3])
fprintf(stderr, "(%f, %f, %f)\n", v[0], v[1], v[2]);
}
#if 0 // removed for FL, it is in fracviewer.c
#if 0 // removed for FL, it is in fracviewer.c
/* normalizes v */
void normalize(GLfloat v[3])
{
@ -133,14 +132,14 @@ void triagnormal(float v1[3], float v2[3], float v3[3], float norm[3])
float xzlength(float v1[3], float v2[3])
{
return sqrt((v1[0] - v2[0])*(v1[0] - v2[0]) +
(v1[2] - v2[2])*(v1[2] - v2[2]));
return sqrtf((v1[0] - v2[0])*(v1[0] - v2[0]) +
(v1[2] - v2[2])*(v1[2] - v2[2]));
}
float xzslope(float v1[3], float v2[3])
{
return ((v1[0] != v2[0]) ? ((v1[2] - v2[2]) / (v1[0] - v2[0]))
: FLT_MAX);
: FLT_MAX);
}
@ -148,15 +147,15 @@ float xzslope(float v1[3], float v2[3])
/************************ MOUNTAIN STUFF ***********************/
/***************************************************************/
GLfloat DispFactor[MAXLEVEL]; /* Array of what to multiply random number
by for a given level to get midpoint
displacement */
GLfloat DispBias[MAXLEVEL]; /* Array of what to add to random number
before multiplying it by DispFactor */
GLfloat DispFactor[MAXLEVEL + 1]; /* Array of what to multiply random number
by for a given level to get midpoint
displacement */
GLfloat DispBias[MAXLEVEL + 1]; /* Array of what to add to random number
before multiplying it by DispFactor */
#define NUMRANDS 191
float RandTable[NUMRANDS]; /* hash table of random numbers so we can
raise the same midpoints by the same amount */
raise the same midpoints by the same amount */
/* The following are for permitting an edge of a moutain to be */
/* pegged so it won't be displaced up or down. This makes it */
@ -164,7 +163,7 @@ float RandTable[NUMRANDS]; /* hash table of random numbers so we can
GLfloat Verts[3][3], /* Vertices of outside edges of mountain */
Slopes[3]; /* Slopes between these outside edges */
int Pegged[3]; /* Is this edge pegged or not */
int Pegged[3]; /* Is this edge pegged or not */
/*
* Comes up with a new table of random numbers [0,1)
@ -175,19 +174,19 @@ void InitRandTable(unsigned int seed)
srand48((long) seed);
for (i = 0; i < NUMRANDS; i++)
RandTable[i] = drand48() - 0.5;
RandTable[i] = drand48() - 0.5f;
}
/* calculate midpoint and displace it if required */
void Midpoint(GLfloat mid[3], GLfloat v1[3], GLfloat v2[3],
int edge, int level)
int edge, int level)
{
unsigned hash;
mid[0] = (v1[0] + v2[0]) / 2;
mid[1] = (v1[1] + v2[1]) / 2;
mid[2] = (v1[2] + v2[2]) / 2;
if (!Pegged[edge] || (fabs(xzslope(Verts[edge], mid)
if (!Pegged[edge] || (fabs(xzslope(Verts[edge], mid)
- Slopes[edge]) > 0.00001)) {
srand48((int)((v1[0]+v2[0])*23344));
hash = unsigned(drand48() * 7334334);
@ -198,9 +197,9 @@ void Midpoint(GLfloat mid[3], GLfloat v1[3], GLfloat v2[3],
}
/*
* Recursive moutain drawing routine -- from lecture with addition of
* Recursive moutain drawing routine -- from lecture with addition of
* allowing an edge to be pegged. This function requires the above
* globals to be set, as well as the Level global for fractal level
* globals to be set, as well as the Level global for fractal level
*/
static float cutoff = -1;
@ -235,13 +234,13 @@ void FMR(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], int level)
void FractalMountain(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3],
int pegged[3])
{
GLfloat lengths[MAXLEVEL];
GLfloat fraction[8] = { 0.3, 0.3, 0.4, 0.2, 0.3, 0.2, 0.4, 0.4 };
GLfloat bias[8] = { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 };
GLfloat lengths[MAXLEVEL + 1];
GLfloat fraction[8] = { 0.3f, 0.3f, 0.4f, 0.2f, 0.3f, 0.2f, 0.4f, 0.4f };
GLfloat bias[8] = { 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f };
int i;
float avglen = (xzlength(v1, v2) +
float avglen = (xzlength(v1, v2) +
xzlength(v2, v3) +
xzlength(v3, v1) / 3);
xzlength(v3, v1) / 3);
for (i = 0; i < 3; i++) {
Verts[0][i] = v1[i]; /* set mountain vertex globals */
@ -254,15 +253,15 @@ void FractalMountain(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3],
Slopes[1] = xzslope(Verts[1], Verts[2]);
Slopes[2] = xzslope(Verts[2], Verts[0]);
lengths[0] = avglen;
for (i = 1; i < Level; i++) {
lengths[0] = avglen;
for (i = 1; i < Level; i++) {
lengths[i] = lengths[i-1]/2; /* compute edge length for each level */
}
for (i = 0; i < Level; i++) { /* DispFactor and DispBias arrays */
for (i = 0; i < Level; i++) { /* DispFactor and DispBias arrays */
DispFactor[i] = (lengths[i] * ((i <= 7) ? fraction[i] : fraction[7]));
DispBias[i] = ((i <= 7) ? bias[i] : bias[7]);
}
}
glBegin(GL_TRIANGLES);
FMR(v1, v2, v3, 0); /* issues no GL but vertex calls */
@ -290,7 +289,7 @@ void CreateMountain(void)
*/
void NewMountain(void)
{
InitRandTable(time(NULL));
InitRandTable((unsigned int)time(NULL));
}
/***************************************************************/
@ -301,7 +300,7 @@ long TreeSeed; /* for srand48 - remember so we can build "same tree"
at a different level */
/*
* recursive tree drawing thing, fleshed out from class notes pseudocode
* recursive tree drawing thing, fleshed out from class notes pseudocode
*/
void FractalTree(int level, long level_seed)
{
@ -313,28 +312,28 @@ void FractalTree(int level, long level_seed)
} else {
glCallList(STEM);
glPushMatrix();
glRotatef(drand48()*180, 0, 1, 0);
glTranslatef(0, 1, 0);
glScalef(0.7, 0.7, 0.7);
glRotatef(drand48()*180.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 1.0f, 0.0f);
glScalef(0.7f, 0.7f, 0.7f);
srand48(level_seed+1);
glPushMatrix();
glRotatef(110 + drand48()*40, 0, 1, 0);
glRotatef(30 + drand48()*20, 0, 0, 1);
glPushMatrix();
glRotatef(110.0f + drand48()*40.0f, 0.0f, 1.0f, 0.0f);
glRotatef( 30.0f + drand48()*20.0f, 0.0f, 0.0f, 1.0f);
FractalTree(level + 1, level_seed+4);
glPopMatrix();
srand48(level_seed+2);
glPushMatrix();
glRotatef(-130 + drand48()*40, 0, 1, 0);
glRotatef(30 + drand48()*20, 0, 0, 1);
glRotatef(-130.0f + drand48()*40.0f, 0.0f, 1.0f, 0.0f);
glRotatef( 30.0f + drand48()*20.0f, 0.0f, 0.0f, 1.0f);
FractalTree(level + 1, level_seed+5);
glPopMatrix();
srand48(level_seed+3);
glPushMatrix();
glRotatef(-20 + drand48()*40, 0, 1, 0);
glRotatef(30 + drand48()*20, 0, 0, 1);
glRotatef(-20.0f + drand48()*40.0f, 0.0f, 1.0f, 0.0f);
glRotatef( 30.0f + drand48()*20.0f, 0.0f, 0.0f, 1.0f);
FractalTree(level + 1, level_seed+6);
glPopMatrix();
@ -352,22 +351,22 @@ void CreateTreeLists(void)
glNewList(STEM, GL_COMPILE);
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gluCylinder(cylquad, 0.1, 0.08, 1, 10, 2 );
glPopMatrix();
glEndList();
glNewList(LEAF, GL_COMPILE); /* I think this was jeff allen's leaf idea */
glBegin(GL_TRIANGLES);
glNormal3f(-0.1, 0, 0.25); /* not normalized */
glVertex3f(0, 0, 0);
glVertex3f(0.25, 0.25, 0.1);
glVertex3f(0, 0.5, 0);
glNormal3f(-0.1f, 0.00f, 0.25f); /* not normalized */
glVertex3f( 0.0f, 0.00f, 0.00f);
glVertex3f(0.25f, 0.25f, 0.10f);
glVertex3f(0.00f, 0.50f, 0.00f);
glNormal3f(0.1, 0, 0.25);
glVertex3f(0, 0, 0);
glVertex3f(0, 0.5, 0);
glVertex3f(-0.25, 0.25, 0.1);
glNormal3f( 0.10f, 0.00f, 0.25f);
glVertex3f( 0.00f, 0.00f, 0.00f);
glVertex3f( 0.00f, 0.50f, 0.00f);
glVertex3f(-0.25f, 0.25f, 0.10f);
glEnd();
glEndList();
@ -377,16 +376,16 @@ void CreateTreeLists(void)
glCallList(STEM);
glCallList(LEAF_MAT);
for(i = 0; i < 3; i++) {
glTranslatef(0, 0.333, 0);
glRotatef(90, 0, 1, 0);
glTranslatef(0.0f, 0.333f, 0.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
glPushMatrix();
glRotatef(0, 0, 1, 0);
glRotatef(50, 1, 0, 0);
glRotatef( 0.0f, 0.0f, 1.0f, 0.0f);
glRotatef(50.0f, 1.0f, 0.0f, 0.0f);
glCallList(LEAF);
glPopMatrix();
glPushMatrix();
glRotatef(180, 0, 1, 0);
glRotatef(60, 1, 0, 0);
glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
glRotatef( 60.0f, 1.0f, 0.0f, 0.0f);
glCallList(LEAF);
glPopMatrix();
}
@ -412,7 +411,7 @@ void CreateTree(void)
FractalTree(0, TreeSeed);
glPopAttrib();
glPopMatrix();
glEndList();
glEndList();
}
/*
@ -420,7 +419,7 @@ void CreateTree(void)
*/
void NewTree(void)
{
TreeSeed = time(NULL);
TreeSeed = long(time(NULL)); // use time() as random seed
}
/***************************************************************/
@ -429,7 +428,7 @@ void NewTree(void)
void CreateIsland(void)
{
cutoff = .06;
cutoff = .06f;
CreateMountain();
cutoff = -1;
glNewList(ISLAND, GL_COMPILE);
@ -439,45 +438,45 @@ void CreateIsland(void)
glCallList(WATER_MAT);
glBegin(GL_QUADS);
glNormal3f(0, 1, 0);
glVertex3f(10, 0.01, 10);
glVertex3f(10, 0.01, -10);
glVertex3f(-10, 0.01, -10);
glVertex3f(-10, 0.01, 10);
glNormal3f( 0.0f, 1.00f, 0.0f);
glVertex3f( 10.0f, 0.01f, 10.0f);
glVertex3f( 10.0f, 0.01f, -10.0f);
glVertex3f(-10.0f, 0.01f, -10.0f);
glVertex3f(-10.0f, 0.01f, 10.0f);
glEnd();
glPushMatrix();
glTranslatef(0, -0.1, 0);
glTranslatef(0.0f, -0.1f, 0.0f);
glCallList(MOUNTAIN);
glPopMatrix();
glPushMatrix();
glRotatef(135, 0, 1, 0);
glTranslatef(0.2, -0.15, -0.4);
glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(0.2f, -0.15f, -0.4f);
glCallList(MOUNTAIN);
glPopMatrix();
glPushMatrix();
glRotatef(-60, 0, 1, 0);
glTranslatef(0.7, -0.07, 0.5);
glRotatef(-60.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(0.7f, -0.07f, 0.5f);
glCallList(MOUNTAIN);
glPopMatrix();
glPushMatrix();
glRotatef(-175, 0, 1, 0);
glTranslatef(-0.7, -0.05, -0.5);
glRotatef(-175.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(-0.7f, -0.05f, -0.5f);
glCallList(MOUNTAIN);
glPopMatrix();
glPushMatrix();
glRotatef(165, 0, 1, 0);
glTranslatef(-0.9, -0.12, 0.0);
glRotatef(165.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(-0.9f, -0.12f, 0.0f);
glCallList(MOUNTAIN);
glPopMatrix();
glPopMatrix();
glPopAttrib();
glEndList();
glEndList();
}
@ -511,21 +510,21 @@ void Create(int fract)
void SetupMaterials(void)
{
GLfloat mtn_ambuse[] = { 0.426, 0.256, 0.108, 1.0 };
GLfloat mtn_specular[] = { 0.394, 0.272, 0.167, 1.0 };
GLfloat mtn_shininess[] = { 10 };
GLfloat mtn_ambuse[] = { 0.426f, 0.256f, 0.108f, 1.0f };
GLfloat mtn_specular[] = { 0.394f, 0.272f, 0.167f, 1.0f };
GLfloat mtn_shininess[] = { 10.0f };
GLfloat water_ambuse[] = { 0.0, 0.1, 0.5, 1.0 };
GLfloat water_specular[] = { 0.0, 0.1, 0.5, 1.0 };
GLfloat water_shininess[] = { 10 };
GLfloat water_ambuse[] = { 0.0f, 0.1f, 0.5f, 1.0f };
GLfloat water_specular[] = { 0.0f, 0.1f, 0.5f, 1.0f };
GLfloat water_shininess[] = { 10.0f };
GLfloat tree_ambuse[] = { 0.4, 0.25, 0.1, 1.0 };
GLfloat tree_specular[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat tree_shininess[] = { 0 };
GLfloat tree_ambuse[] = { 0.4f, 0.25f, 0.1f, 1.0f };
GLfloat tree_specular[] = { 0.0f, 0.00f, 0.0f, 1.0f };
GLfloat tree_shininess[] = { 0.0f };
GLfloat leaf_ambuse[] = { 0.0, 0.8, 0.0, 1.0 };
GLfloat leaf_specular[] = { 0.0, 0.8, 0.0, 1.0 };
GLfloat leaf_shininess[] = { 10 };
GLfloat leaf_ambuse[] = { 0.0f, 0.8f, 0.0f, 1.0f };
GLfloat leaf_specular[] = { 0.0f, 0.8f, 0.0f, 1.0f };
GLfloat leaf_shininess[] = { 10.0f };
glNewList(MOUNTAIN_MAT, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mtn_ambuse);
@ -554,18 +553,18 @@ void SetupMaterials(void)
void myGLInit(void)
{
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.3, 0.3, 0.0 };
GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_position[] = { 0.0f, 0.3f, 0.3f, 0.0f };
GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat lmodel_ambient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
@ -590,7 +589,7 @@ void myGLInit(void)
CreateTreeLists();
glFlush();
}
}
/***************************************************************/
/************************ GLUT STUFF ***************************/
@ -639,7 +638,7 @@ void display(void)
glLoadIdentity();
gluOrtho2D(0.0, winwidth, 0.0, winheight);
sprintf(buf, "FPS=%d", fps);
snprintf(buf, sizeof(buf), "FPS=%d", fps);
glColor3f(1.0f, 1.0f, 1.0f);
gl_font(FL_HELVETICA, 12);
gl_draw(buf, 10, 10);
@ -656,7 +655,7 @@ void display(void)
curtime = time(NULL);
if ((curtime - fpstime) >= 2)
{
fps = (fps + fpscount / (curtime - fpstime)) / 2;
fps = (fps + fpscount / int(curtime - fpstime)) / 2;
fpstime = curtime;
fpscount = 0;
}
@ -792,7 +791,7 @@ int main(int argc, char** argv)
g->end();
b = new Fl_Button(400,50,100,30,"New Fractal"); b->callback(handlemenu,(void*)MENU_RAND);
b = new Fl_Button( 10,10,100,30,"Mountain"); b->callback(choosefract,(void*)MOUNTAIN);
b = new Fl_Button(110,10,100,30,"Tree"); b->callback(choosefract,(void*)TREE);
b = new Fl_Button(210,10,100,30,"Island"); b->callback(choosefract,(void*)ISLAND);
@ -817,7 +816,7 @@ int main(int argc, char** argv)
NewFractals();
agvMakeAxesList(AXES);
myGLInit();
myGLInit();
MenuInit();
glutMainLoop(); // you could use Fl::run() instead
@ -825,7 +824,3 @@ int main(int argc, char** argv)
return 0;
}
#endif
//
// End of "$Id$".
//