Browse Source

Add initial examples

gh-pages
falkTX 11 years ago
parent
commit
49b2d5e95c
11 changed files with 7793 additions and 0 deletions
  1. +8
    -0
      .gitignore
  2. +58
    -0
      examples/Makefile
  3. +43
    -0
      examples/app.cpp
  4. +154
    -0
      examples/color.cpp
  5. +69
    -0
      examples/image.cpp
  6. +54
    -0
      examples/nekobi-ui.cpp
  7. +6890
    -0
      examples/nekobi-ui_src/DistrhoArtworkNekobi.cpp
  8. +90
    -0
      examples/nekobi-ui_src/DistrhoArtworkNekobi.hpp
  9. +142
    -0
      examples/nekobi-ui_src/DistrhoUINekobi.cpp
  10. +81
    -0
      examples/nekobi-ui_src/DistrhoUINekobi.hpp
  11. +204
    -0
      examples/nekobi-ui_src/NekoWidget.hpp

+ 8
- 0
.gitignore View File

@@ -1,6 +1,14 @@
*.a
*.o

*.exe
*.dll
*.dylib
*.so

.kdev_include_paths

examples/app
examples/color
examples/image
examples/nekobi-ui

+ 58
- 0
examples/Makefile View File

@@ -0,0 +1,58 @@
#!/usr/bin/make -f
# Makefile for DPT examples #
# ------------------------- #
# Created by falkTX
#

include ../dgl/Makefile.mk

# --------------------------------------------------------------

BUILD_CXX_FLAGS += -I../dgl
LINK_FLAGS += -L.. -ldgl $(DGL_LIBS)

# --------------------------------------------------------------

ifeq ($(WIN32),true)
TARGETS = app.exe image.exe nekobi-ui.exe
else
TARGETS = app image nekobi-ui
endif

# --------------------------------------------------------------

all: ../libdgl.a $(TARGETS)
# --------------------------------------------------------------

%.exe: %
mv $* $*.exe

../libdgl.a: .FORCE
$(MAKE) -C ../dgl

# --------------------------------------------------------------

app: app.cpp ../dgl/*
$(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@

color: color.cpp ../dgl/*
$(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@

image: image.cpp ../dgl/*
$(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@

nekobi-ui: nekobi-ui.cpp nekobi-ui_src/* ../dgl/*
$(CXX) $< $(BUILD_CXX_FLAGS) $(LINK_FLAGS) -o $@

# --------------------------------------------------------------

clean:
$(RM) $(TARGETS)

debug:
$(MAKE) DEBUG=true

# --------------------------------------------------------------

.FORCE:
.PHONY: .FORCE

+ 43
- 0
examples/app.cpp View File

@@ -0,0 +1,43 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

// ------------------------------------------------------
// DGL Stuff

#include "App.hpp"
#include "Window.hpp"

// ------------------------------------------------------
// use namespace

using namespace DGL;

// ------------------------------------------------------
// main entry point

int main()
{
App app;
Window win(app);

win.setTitle("App");
win.show();
app.exec();

return 0;
}

// ------------------------------------------------------

+ 154
- 0
examples/color.cpp View File

@@ -0,0 +1,154 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

// ------------------------------------------------------
// DGL Stuff

#include "App.hpp"
#include "Window.hpp"
#include "Widget.hpp"

#include <cstdio>

// ------------------------------------------------------
// use namespace

using namespace DGL;

// ------------------------------------------------------
// Single color widget

class ColorWidget : public App::IdleCallback,
Widget
{
public:
ColorWidget(Window& parent)
: Widget(parent),
cur('r'),
reverse(false),
r(0), g(0), b(0)
{
}

void idleCallback() override
{
switch (cur)
{
case 'r':
if (reverse)
{
if (--r == 0)
cur = 'g';
}
else
{
if (++r == 100)
cur = 'g';
}
break;

case 'g':
if (reverse)
{
if (--g == 0)
cur = 'b';
}
else
{
if (++g == 100)
cur = 'b';
}
break;

case 'b':
if (reverse)
{
if (--b == 0)
{
cur = 'r';
reverse = false;
}
}
else
{
if (++b == 100)
{
cur = 'r';
reverse = true;
}
}
break;
}

repaint();
}

private:
void onDisplay() override
{
glColor3b(r, g, b);

// full size
const int width = getWidth();
const int height = getHeight();

glBegin(GL_QUADS);
glTexCoord2i(0, height);
glVertex2i(0, height);

glTexCoord2i(width, height);
glVertex2i(width, height);

glTexCoord2i(width, 0);
glVertex2i(width, 0);

glTexCoord2i(0, 0);
glVertex2i(0, 0);
glEnd();
}

void onReshape(int width, int height) override
{
// make widget same size as window
setSize(width, height);
Widget::onReshape(width, height);
}

char cur;
bool reverse;
int r, g, b;
};

// ------------------------------------------------------
// main entry point

int main()
{
App app;
Window win(app);
ColorWidget color(win);

app.addIdleCallback(&color);

win.setSize(300, 300);
win.setTitle("Color");
win.show();
app.exec();

return 0;
}

// ------------------------------------------------------

+ 69
- 0
examples/image.cpp View File

@@ -0,0 +1,69 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

// ------------------------------------------------------
// DGL Image

#include "Image.hpp"

// ------------------------------------------------------
// DGL Widget and StandaloneWindow

#include "Widget.hpp"
#include "StandaloneWindow.hpp"

// ------------------------------------------------------
// use namespace

using namespace DGL;

// ------------------------------------------------------
// our widget

class ExampleImageWidget : public Widget
{
public:
ExampleImageWidget(Window& win)
: Widget(win)
{
}

protected:
void onDisplay() override
{
}
};

// ------------------------------------------------------
// main entry point

int main()
{
StandaloneWindow appWin;
Window& win(appWin.getWindow());

ExampleImageWidget gui(win);
win.setResizable(false);
win.setSize(200, 200);
win.setTitle("Image");
win.show();

appWin.exec();

return 0;
}

// ------------------------------------------------------

+ 54
- 0
examples/nekobi-ui.cpp View File

@@ -0,0 +1,54 @@
/*
* DISTRHO Plugin Toolkit (DPT)
* Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

// ------------------------------------------------------
// DGL StandaloneWindow

#include "StandaloneWindow.hpp"

// ------------------------------------------------------
// include all source code here to make building easier

#include "nekobi-ui_src/DistrhoArtworkNekobi.cpp"
#include "nekobi-ui_src/DistrhoUINekobi.cpp"

// ------------------------------------------------------
// main entry point

int main()
{
DGL::StandaloneWindow appWin;
DGL::Window& win(appWin.getWindow());

DistrhoUINekobi gui(win);
win.setResizable(false);
win.setSize(gui.getWidth(), gui.getHeight());
win.setTitle("DGL UI Test");
win.show();

DGL::App& app(appWin.getApp());

while (! app.isQuiting())
{
gui.idle();
app.idle();
msleep(10);
}

return 0;
}

// ------------------------------------------------------

+ 6890
- 0
examples/nekobi-ui_src/DistrhoArtworkNekobi.cpp
File diff suppressed because it is too large
View File


+ 90
- 0
examples/nekobi-ui_src/DistrhoArtworkNekobi.hpp View File

@@ -0,0 +1,90 @@
/* (Auto-generated binary data file). */

#ifndef BINARY_DISTRHOARTWORKNEKOBI_HPP
#define BINARY_DISTRHOARTWORKNEKOBI_HPP

namespace DistrhoArtworkNekobi
{
extern const char* aboutData;
const unsigned int aboutDataSize = 172710;
const unsigned int aboutWidth = 303;
const unsigned int aboutHeight = 190;

extern const char* aboutButtonHoverData;
const unsigned int aboutButtonHoverDataSize = 5888;
const unsigned int aboutButtonHoverWidth = 92;
const unsigned int aboutButtonHoverHeight = 16;

extern const char* aboutButtonNormalData;
const unsigned int aboutButtonNormalDataSize = 5888;
const unsigned int aboutButtonNormalWidth = 92;
const unsigned int aboutButtonNormalHeight = 16;

extern const char* backgroundData;
const unsigned int backgroundDataSize = 206064;
const unsigned int backgroundWidth = 636;
const unsigned int backgroundHeight = 108;

extern const char* claw1Data;
const unsigned int claw1DataSize = 4096;
const unsigned int claw1Width = 32;
const unsigned int claw1Height = 32;

extern const char* claw2Data;
const unsigned int claw2DataSize = 4096;
const unsigned int claw2Width = 32;
const unsigned int claw2Height = 32;

extern const char* knobData;
const unsigned int knobDataSize = 10000;
const unsigned int knobWidth = 50;
const unsigned int knobHeight = 50;

extern const char* run1Data;
const unsigned int run1DataSize = 4096;
const unsigned int run1Width = 32;
const unsigned int run1Height = 32;

extern const char* run2Data;
const unsigned int run2DataSize = 4096;
const unsigned int run2Width = 32;
const unsigned int run2Height = 32;

extern const char* run3Data;
const unsigned int run3DataSize = 4096;
const unsigned int run3Width = 32;
const unsigned int run3Height = 32;

extern const char* run4Data;
const unsigned int run4DataSize = 4096;
const unsigned int run4Width = 32;
const unsigned int run4Height = 32;

extern const char* scratch1Data;
const unsigned int scratch1DataSize = 4096;
const unsigned int scratch1Width = 32;
const unsigned int scratch1Height = 32;

extern const char* scratch2Data;
const unsigned int scratch2DataSize = 4096;
const unsigned int scratch2Width = 32;
const unsigned int scratch2Height = 32;

extern const char* sitData;
const unsigned int sitDataSize = 4096;
const unsigned int sitWidth = 32;
const unsigned int sitHeight = 32;

extern const char* sliderData;
const unsigned int sliderDataSize = 6084;
const unsigned int sliderWidth = 39;
const unsigned int sliderHeight = 39;

extern const char* tailData;
const unsigned int tailDataSize = 4096;
const unsigned int tailWidth = 32;
const unsigned int tailHeight = 32;
}

#endif // BINARY_DISTRHOARTWORKNEKOBI_HPP


+ 142
- 0
examples/nekobi-ui_src/DistrhoUINekobi.cpp View File

@@ -0,0 +1,142 @@
/*
* DISTRHO Nekobi Plugin, based on Nekobee by Sean Bolton and others.
* Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/

#include "DistrhoUINekobi.hpp"

using DGL::Point;

// -----------------------------------------------------------------------

DistrhoUINekobi::DistrhoUINekobi(DGL::Window& parent)
: DGL::Widget(parent),
fAboutWindow(this)
{
fNeko.setTimerSpeed(15);

// background
fImgBackground = Image(DistrhoArtworkNekobi::backgroundData, DistrhoArtworkNekobi::backgroundWidth, DistrhoArtworkNekobi::backgroundHeight, GL_BGR);

Image imageAbout(DistrhoArtworkNekobi::aboutData, DistrhoArtworkNekobi::aboutWidth, DistrhoArtworkNekobi::aboutHeight, GL_BGR);
fAboutWindow.setImage(imageAbout);

// slider
Image sliderImage(DistrhoArtworkNekobi::sliderData, DistrhoArtworkNekobi::sliderWidth, DistrhoArtworkNekobi::sliderHeight);

fSliderWaveform = new ImageSlider(this, sliderImage);
fSliderWaveform->setStartPos(133, 40);
fSliderWaveform->setEndPos(133, 60);
fSliderWaveform->setRange(0.0f, 1.0f);
fSliderWaveform->setValue(0.0f);
fSliderWaveform->setIsSwitch(true);

// knobs
Image knobImage(DistrhoArtworkNekobi::knobData, DistrhoArtworkNekobi::knobWidth, DistrhoArtworkNekobi::knobHeight);

// knob Tuning
fKnobTuning = new ImageKnob(this, knobImage);
fKnobTuning->setPos(41, 43);
fKnobTuning->setRange(-12.0f, 12.0f);
fKnobTuning->setValue(0.0f);
fKnobTuning->setRotationAngle(305);

// knob Cutoff
fKnobCutoff = new ImageKnob(this, knobImage);
fKnobCutoff->setPos(185, 43);
fKnobCutoff->setRange(0.0f, 100.0f);
fKnobCutoff->setValue(25.0f);
fKnobCutoff->setRotationAngle(305);

// knob Resonance
fKnobResonance = new ImageKnob(this, knobImage);
fKnobResonance->setPos(257, 43);
fKnobResonance->setRange(0.0f, 95.0f);
fKnobResonance->setValue(25.0f);
fKnobResonance->setRotationAngle(305);

// knob Env Mod
fKnobEnvMod = new ImageKnob(this, knobImage);
fKnobEnvMod->setPos(329, 43);
fKnobEnvMod->setRange(0.0f, 100.0f);
fKnobEnvMod->setValue(50.0f);
fKnobEnvMod->setRotationAngle(305);

// knob Decay
fKnobDecay = new ImageKnob(this, knobImage);
fKnobDecay->setPos(400, 43);
fKnobDecay->setRange(0.0f, 100.0f);
fKnobDecay->setValue(75.0f);
fKnobDecay->setRotationAngle(305);

// knob Accent
fKnobAccent = new ImageKnob(this, knobImage);
fKnobAccent->setPos(473, 43);
fKnobAccent->setRange(0.0f, 100.0f);
fKnobAccent->setValue(25.0f);
fKnobAccent->setRotationAngle(305);

// knob Volume
fKnobVolume = new ImageKnob(this, knobImage);
fKnobVolume->setPos(545, 43);
fKnobVolume->setRange(0.0f, 100.0f);
fKnobVolume->setValue(75.0f);
fKnobVolume->setRotationAngle(305);

// about button
Image aboutImageNormal(DistrhoArtworkNekobi::aboutButtonNormalData, DistrhoArtworkNekobi::aboutButtonNormalWidth, DistrhoArtworkNekobi::aboutButtonNormalHeight);
Image aboutImageHover(DistrhoArtworkNekobi::aboutButtonHoverData, DistrhoArtworkNekobi::aboutButtonHoverWidth, DistrhoArtworkNekobi::aboutButtonHoverHeight);
fButtonAbout = new ImageButton(this, aboutImageNormal, aboutImageHover, aboutImageHover);
fButtonAbout->setPos(505, 5);
fButtonAbout->setCallback(this);
}

DistrhoUINekobi::~DistrhoUINekobi()
{
delete fSliderWaveform;
delete fKnobTuning;
delete fKnobCutoff;
delete fKnobResonance;
delete fKnobEnvMod;
delete fKnobDecay;
delete fKnobAccent;
delete fKnobVolume;
delete fButtonAbout;
}

void DistrhoUINekobi::idle()
{
if (fNeko.idle())
repaint();
}

// -----------------------------------------------------------------------
// Widget Callbacks

void DistrhoUINekobi::imageButtonClicked(ImageButton* button, int)
{
if (button != fButtonAbout)
return;

fAboutWindow.exec();
}

void DistrhoUINekobi::onDisplay()
{
fImgBackground.draw();
fNeko.draw();
}

// -----------------------------------------------------------------------

+ 81
- 0
examples/nekobi-ui_src/DistrhoUINekobi.hpp View File

@@ -0,0 +1,81 @@
/*
* DISTRHO Nekobi Plugin, based on Nekobee by Sean Bolton and others.
* Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/

#ifndef DISTRHO_UI_NEKOBI_HPP_INCLUDED
#define DISTRHO_UI_NEKOBI_HPP_INCLUDED

#include "ImageAboutWindow.hpp"
#include "ImageButton.hpp"
#include "ImageKnob.hpp"
#include "ImageSlider.hpp"

#include "DistrhoArtworkNekobi.hpp"
#include "NekoWidget.hpp"

using DGL::ImageAboutWindow;
using DGL::ImageButton;
using DGL::ImageKnob;
using DGL::ImageSlider;

// -----------------------------------------------------------------------

class DistrhoUINekobi : public DGL::Widget,
public ImageButton::Callback
{
public:
DistrhoUINekobi(DGL::Window& parent);
~DistrhoUINekobi() override;

unsigned int getWidth() const noexcept
{
return DistrhoArtworkNekobi::backgroundWidth;
}

unsigned int getHeight() const noexcept
{
return DistrhoArtworkNekobi::backgroundHeight;
}

void idle();

protected:
// -------------------------------------------------------------------
// Widget Callbacks

void imageButtonClicked(ImageButton* button, int) override;
void onDisplay() override;

private:
Image fImgBackground;
NekoWidget fNeko;

ImageKnob* fKnobTuning;
ImageKnob* fKnobCutoff;
ImageKnob* fKnobResonance;
ImageKnob* fKnobEnvMod;
ImageKnob* fKnobDecay;
ImageKnob* fKnobAccent;
ImageKnob* fKnobVolume;

ImageButton* fButtonAbout;
ImageSlider* fSliderWaveform;
ImageAboutWindow fAboutWindow;
};

// -----------------------------------------------------------------------

#endif // DISTRHO_UI_NEKOBI_HPP_INCLUDED

+ 204
- 0
examples/nekobi-ui_src/NekoWidget.hpp View File

@@ -0,0 +1,204 @@
/*
* Neko widget animation
* Copyright (C) 2013 Filipe Coelho <falktx@falktx.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the doc/GPL.txt file.
*/

#ifndef NEKO_WIDGET_HPP_INCLUDED
#define NEKO_WIDGET_HPP_INCLUDED

#include "Image.hpp"
#include "Widget.hpp"

#include <cstdlib> // rand

#include "DistrhoArtworkNekobi.hpp"

using DGL::Image;

USE_NAMESPACE_DGL;

// -----------------------------------------------------------------------

class NekoWidget
{
public:
NekoWidget()
: fPos(0),
fTimer(0),
fTimerSpeed(20),
fCurAction(kActionNone),
fCurImage(&fImages.sit)
{
// load images
{
using namespace DistrhoArtworkNekobi;

#define JOIN(a, b) a ## b
#define LOAD_IMAGE(NAME) fImages.NAME.loadFromMemory(JOIN(NAME, Data), JOIN(NAME, Width), JOIN(NAME, Height));

LOAD_IMAGE(sit)
LOAD_IMAGE(tail)
LOAD_IMAGE(claw1)
LOAD_IMAGE(claw2)
LOAD_IMAGE(scratch1)
LOAD_IMAGE(scratch2)
LOAD_IMAGE(run1)
LOAD_IMAGE(run2)
LOAD_IMAGE(run3)
LOAD_IMAGE(run4)

#undef JOIN
#undef LOAD_IMAGE
}
}

void draw()
{
int x = fPos+108;
int y = -2;

if (fCurImage == &fImages.claw1 || fCurImage == &fImages.claw2)
{
x += 2;
y += 12;
}

fCurImage->draw(x, y);
}

// returns true if needs repaint
bool idle()
{
if (++fTimer % fTimerSpeed != 0) // target is 20ms
return false;

if (fTimer == fTimerSpeed*9)
{
if (fCurAction == kActionNone)
fCurAction = static_cast<Action>(std::rand() % kActionCount);
else
fCurAction = kActionNone;

fTimer = 0;
}

switch (fCurAction)
{
case kActionNone:
if (fCurImage == &fImages.sit)
fCurImage = &fImages.tail;
else
fCurImage = &fImages.sit;
break;

case kActionClaw:
if (fCurImage == &fImages.claw1)
fCurImage = &fImages.claw2;
else
fCurImage = &fImages.claw1;
break;

case kActionScratch:
if (fCurImage == &fImages.scratch1)
fCurImage = &fImages.scratch2;
else
fCurImage = &fImages.scratch1;
break;

case kActionRunRight:
if (fTimer == 0 && fPos > 20*9)
{
// run the other way
--fTimer;
fCurAction = kActionRunLeft;
idle();
break;
}

fPos += 20;

if (fCurImage == &fImages.run1)
fCurImage = &fImages.run2;
else
fCurImage = &fImages.run1;
break;

case kActionRunLeft:
if (fTimer == 0 && fPos < 20*9)
{
// run the other way
--fTimer;
fCurAction = kActionRunRight;
idle();
break;
}

fPos -= 20;

if (fCurImage == &fImages.run3)
fCurImage = &fImages.run4;
else
fCurImage = &fImages.run3;
break;

case kActionCount:
break;
}

return true;
}

void setTimerSpeed(int speed)
{
fTimer = 0;
fTimerSpeed = speed;
}

// -------------------------------------------------------------------

private:
enum Action {
kActionNone, // bounce tail
kActionClaw,
kActionScratch,
kActionRunRight,
kActionRunLeft,
kActionCount
};

struct Images {
Image sit;
Image tail;
Image claw1;
Image claw2;
Image scratch1;
Image scratch2;
Image run1;
Image run2;
Image run3;
Image run4;
} fImages;

int fPos;
int fTimer;
int fTimerSpeed;

Action fCurAction;
Image* fCurImage;
};

// -----------------------------------------------------------------------

#endif // NEKO_WIDGET_HPP_INCLUDED

Loading…
Cancel
Save